How to declare a templated function with an optional compile-time parameter?

Template void foo (Type2 arg) { ... code ... } template void foo (Type2 arg) { foo(arg); } Edit: The above snippet works, but has the drawback that Type2 needs to be explicitely specified in the calls I have to admit that I can't think of a good full-template solution for this; the closest I could get was using empty method arguments: struct DefaultOption { ... } DEFAULT; struct OtherOption { ... } OTHER; template void foo (Type2 arg, Option) { ... code ... } template void foo (Type2 arg) { foo(arg, DEFAULT); } This allows calls in the form foo(1, DEFAULT); foo(1.0, OTHER); foo("") I'm curious as to what the real answer to this puzzle is.

Template void foo (Type2 arg) { ... code ... } template void foo (Type2 arg) { foo(arg); } Edit: The above snippet works, but has the drawback that Type2 needs to be explicitely specified in the calls. I have to admit that I can't think of a good full-template solution for this; the closest I could get was using empty method arguments: struct DefaultOption { ... } DEFAULT; struct OtherOption { ... } OTHER; template void foo (Type2 arg, Option) { ... code ... } template void foo (Type2 arg) { foo(arg, DEFAULT); } This allows calls in the form foo(1, DEFAULT); foo(1.0, OTHER); foo(" I'm curious as to what the real answer to this puzzle is.

Close to what I want but not quite. It want to be able to call foo like this: foo(42), or foo(42. F).

– deft_code Dec 4 '10 at 0:58 Indeed. I edited my answer accordingly, but sadly it's still not quite what you have in mind. – Lars Dec 4 '10 at 1:24 3 Can't you switch the order of template arguments?

Template? – UncleBens Dec 4 '10 at 10:03 duh That works! Write it up as an answer, so you can get credit for it.

– Lars Dec 4 '10 at 0:52.

You could always try template struct A { template void func( Type2 t2 ) { // function body } }; Maybe this is what you need.

I over thought the problem and confused everyone else as well. It's not possible to write one function unless C++0x extensions are used. However it's quite straight forward to write it with two overloaded functions.

Template void func( Type2 t2 ) { /* ... */ } template void func( Type2 t2 ) { func(t2); } func(20. F); func(30); // uses Default as Option func(30); //exact same call as above.

If I understood you correctly, you need to use reflection. C++ has a reflection API library as far as I remember. If you don't know about reflective paradigm, I think you should learn about it first.

2 C++ doesn't have reflection. (It also shouldn't be necessary for something like this) – jalf Dec 4 '10 at 4:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions