function template type deduction from default parameter

T

tobias.loew

Hi,

I wanted my compiler (MSVC 9, VS2008) to deduce the template type of a function template from a default argument, but it doesn't work.

template< class _dummy >
void test( _dummy d = false )
{
....
}

void foo()
{
test(); // <-- error C2783: "void test(_dummy)": could not deduce template argument for "_dummy"
}


So I looked into the standard (N3337) but I couldn't find anything specific for this case (neither at 14.8.2.1 nor at 8.3.5).

Is there any reason, why this is not possible, or is it just an error in my compiler?

regards

Tobias
 
V

Victor Bazarov

Hi,

I wanted my compiler (MSVC 9, VS2008) to deduce the template type of a function template from a default argument, but it doesn't work.

template< class _dummy >
void test( _dummy d = false )
{
...
}

void foo()
{
test(); // <-- error C2783: "void test(_dummy)": could not deduce template argument for "_dummy"
}


So I looked into the standard (N3337) but I couldn't find anything specific for this case (neither at 14.8.2.1 nor at 8.3.5).

Is there any reason, why this is not possible, or is it just an error in my compiler?

I have found one reference to the default argument values AFA template
argument deduction is concerned. It's in the partial ordering section
of the clause 14.

14.8.2.4 Deducing template arguments during partial ordering
[temp.deduct.partial]
[...]
3 The types used to determine the ordering depend on the context in
which the partial ordering is done:
— In the context of a function call, the types used are those function
parameter types are used. for which
the function call has arguments.<footnote 146> [...]
---
146) Default arguments are not considered to be arguments in this
context; they only become arguments after a function has
been selected.

I think there is a strong hint as to why 'bool' has no meaning in your
case -- first the function has to be figured out, then its default
arguments start playing the role, not before the function is actually found.

BTW, unless this case is purely academic, theoretical, what problem were
you solving that led you to this question? What is the reason that you
have 'false' as the default, and not, say, '0'? Or, say, 'nullptr'?

V
 
T

tobias.loew

Thanks for the hint.
The problem was the following:
I wanted "test" to be an inline, non-template function in a header file, but then I had to put additional includes into the file for satisfying the stuff I used in test's body.
So, I made it a template function to be weak bound, but I didn't want to change the function calls by adding angle-brackets or dummy arguments.
As MSVC9 (i.e. C++ 03) doesn't allow default arguments for function templates, I tried it using the default argument.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top