Static Duck Typing

2 views
Skip to first unread message

Dean Michael Berris

unread,
Nov 26, 2006, 9:16:50 PM11/26/06
to phil...@googlegroups.com
Hi Everyone,

I just wanted to share how all the perceived confusion about static
typing being a bad thing is usually trumped by a compiler feature in
C++ called argument type deduction. Although this still happens during
compile time (as opposed to runtime with other languages), it
shouldn't make much difference because in runtime languages you do
have to instigate a recompile albeit automatically when then source
changes.

An example would be creating free functions that adhered by an
interface instead of a type like:

template <typename T>
void swap(T & left, T & right);

When using these functions though, you don't have to explicitly fill
in the template parameters: instead have the compiler do it for you:

int a = 1, b = 2;
double c = 1.0, d = 2.0;
float e = 1.0f, f = 2.0f
swap(a, b);
swap(c, d);
swap(e, f);

This technique allows for writing generic and type-safe algorithms and
solutions.

I hope you'll find it useful someday! :)

--
Dean Michael C. Berris
C++ Software Architect
Orange and Bronze Software Labs, Ltd. Co.
web: http://software.orangeandbronze.com/
email: de...@orangeandbronze.com
mobile: +63 928 7291459
phone: +63 2 8943415
other: +1 408 4049532
blogs: http://mikhailberis.blogspot.com http://3w-agility.blogspot.com
http://cplusplus-soup.blogspot.com

me22

unread,
Nov 26, 2006, 9:32:53 PM11/26/06
to phil...@googlegroups.com
On 11/26/06, Dean Michael Berris <mikhai...@gmail.com> wrote:
> I hope you'll find it useful someday! :)
>
As soon as we get auto and some variant of typeof, it'll be even more so =)

~ Scott

Joel de Guzman

unread,
Nov 26, 2006, 9:34:44 PM11/26/06
to phil...@googlegroups.com

don't forget concepts! c++ is waaayyy ahead from the rest.

--
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

Dean Michael Berris

unread,
Nov 26, 2006, 9:51:05 PM11/26/06
to phil...@googlegroups.com

I personally like decltype + auto. Makes for very good fun:

template <typename T1, typename T2>
auto add(T1 const & left, T2 const & right) -> result_of<T1, T2>::type {
};

Or something like that. :)

Dean Michael Berris

unread,
Nov 26, 2006, 9:52:31 PM11/26/06
to phil...@googlegroups.com
On 11/27/06, Joel de Guzman <jo...@boost-consulting.com> wrote:
>
> me22 wrote:
> > On 11/26/06, Dean Michael Berris <mikhai...@gmail.com> wrote:
> >> I hope you'll find it useful someday! :)
> >>
> > As soon as we get auto and some variant of typeof, it'll be even more so =)
>
> don't forget concepts! c++ is waaayyy ahead from the rest.
>

Indeed. :) I hope to write more axioms than abstract types in the future too.

Reply all
Reply to author
Forward
0 new messages