Problem with default parameters in templated functions

  • Thread starter Chris Jefferson
  • Start date
C

Chris Jefferson

I came across an unusual problem recently.. I think that I've can see
why it's done, but it's still annoying. I was just wondering if I should
have this problem (it came up in g++ for me)

Consider the following (very reduced!) piece of code.

struct foo {};

template<typename U>
void bar(U u = foo())
{ }



Now, lets try to use bar:
bar(1); //works
bar<foo>(); //works
bar(); //"No matching function to bar()"

It seems slightly strange that this doesn't work.. at the moment I'm
having to fix this the "old fashioned way", by adding (after removing
the default parameter definition for bar)

void bar()
{ bar(foo()); }

Which works fine.. I'm still not clear on why it shouldn't be able to
perform the deduction however?

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top