By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,646 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,646 IT Pros & Developers. It's quick & easy.

defaults for template'd functions

P: n/a
Hey all,

I've been running into a problem with default values to template'd
functions. I've boiled down my problem to this simple example code:

#include<iostream>

using namespace std;

// function object
template<typename Valuestruct function {
void operator()(Value v) {
cout << '*' << v << '*' << endl;
}
};

template<typename Value, typename Function>
void do_it(Value v, Function f = function<Value>()) {
cout << v << '\t';
f(v);
}

int main(int argc, char* argv[]) {
do_it(3);
return 0;
}

Basically, I want the second parameter of do_it to be anything that can
be called like a function with a single argument of type Value but I
want the default to be the default constructed object of type
function<Value>(). That's what I want, but what I get is this error:

test.cpp:19: error: no matching function for call to 'do_it(int)'

It doesn't seem to be recognizing my default. If I change the call to
do_it to:

do_it<int, function<int(3);

i.e explicitly give the type of the default in int case, it works fine.
But I don't want to have to give it the default type every time I call
it, then I wouldn't even bother with making it a default.

So, I figured if I give a default for the typename and well as the
value it might work:

template<typename Value, typename Function = function<Value
void do_it(Value v, Function f = function<Value>()) {
cout << v << '\t';
f(v);
}

but then I get this error:

test.cpp:13: error: default template arguments may not be used in
function templates

Any idea why none of this isn't working? Am I asking C++ to do to much
type inference? I also don't understand why I'm getting the error at
the function call and not the function declaration.

P.S. I'm compiling this with gcc version 4.0.1 (Apple Computer, Inc.
build 5250). Here's the full g++ -v:

Using built-in specs.
Target: i686-apple-darwin8
Configured with: /private/var/tmp/gcc/gcc-5250.obj~12/src/configure
--disable-checking -enable-werror --prefix=/usr --mandir=/share/man
--enable-languages=c,objc,c++,obj-c++
--program-transform-name=/^[cg][^.-]*$/s/$/-4.0/
--with-gxx-include-dir=/include/c++/4.0.0 --build=powerpc-apple-darwin8
--with-arch=pentium-m --with-tune=prescott --program-prefix=
--host=i686-apple-darwin8 --target=i686-apple-darwin8
Thread model: posix
gcc version 4.0.1 (Apple Computer, Inc. build 5250)

-krish

Jan 13 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
kr***********@gmail.com wrote:
Hey all,

I've been running into a problem with default values to template'd
functions. I've boiled down my problem to this simple example code:

#include<iostream>

using namespace std;

// function object
template<typename Valuestruct function {
void operator()(Value v) {
cout << '*' << v << '*' << endl;
}
};

template<typename Value, typename Function>
void do_it(Value v, Function f = function<Value>()) {
cout << v << '\t';
f(v);
}

int main(int argc, char* argv[]) {
do_it(3);
return 0;
}

Basically, I want the second parameter of do_it to be anything that can
be called like a function with a single argument of type Value but I
want the default to be the default constructed object of type
function<Value>(). That's what I want, but what I get is this error:

test.cpp:19: error: no matching function for call to 'do_it(int)'
The problem is that there is now way to figure out the type of the
second type parameter in the "do_it(3)" function call. After all,
main() calls do_it() with only one argument, 3. So the compiler has to
figure out what type a second argument would have been - if main() had
called do_it() with two arguments (instead the one argument) that
main() passed in the actual call.

Of course, there is no way to ascertain the type of an absent argument
- nor can the program ever be able to answer that question. So another
approach is needed to solve this problem. And that solution would be to
have the program provide a second parameter whenever one is not present
in the call to do_it(). In this way the program can pick whatever type
it likes that argument to be (in the case, the choice will be
function<Value>):

#include <iostream>

using std::cout;
using std::endl;

template< class Value>
struct function
{
void operator()(Value v)
{
cout << '*' << v << '*' << endl;
}
};

template< class Value, class Function>
void do_it( Value v, Function f )
{
cout << v << '\t';
f(v);
}

template< class Value>
void do_it( Value v)
{
do_it( v, function<Value>());
}

int main()
{
do_it(3);
}

With two do_it() overloads, calling do_it() with one argument forwards
the call to the other do_it(), providing its own the Function<Value>
object as the "missing", second parameter. Essentially, the program has
implemented C++'s default argument behavior for the do_it() function
all on its own.

Greg

Jan 13 '07 #2

P: n/a
kr***********@gmail.com wrote:
Hey all,

I've been running into a problem with default values to template'd
functions. I've boiled down my problem to this simple example code:

#include<iostream>

using namespace std;

// function object
template<typename Valuestruct function {
void operator()(Value v) {
cout << '*' << v << '*' << endl;
}
};

template<typename Value, typename Function>
void do_it(Value v, Function f = function<Value>()) {
cout << v << '\t';
f(v);
}

int main(int argc, char* argv[]) {
do_it(3);
return 0;
}

Basically, I want the second parameter of do_it to be anything that can
be called like a function with a single argument of type Value but I
want the default to be the default constructed object of type
function<Value>(). That's what I want, but what I get is this error:

test.cpp:19: error: no matching function for call to 'do_it(int)'
The problem is that there is no way to figure out the type of the
second type parameter in the "do_it(3)" function call. After all,
main() calls do_it() with only one argument, 3. So the compiler has to
figure out what type a second argument would have been - if main() had
called do_it() with two arguments (instead the one argument) that
main() passed in the actual call.

Of course, there is no way to ascertain the type of an absent argument
- nor can the program ever be able to answer that question. So another
approach is needed to solve this problem. And that solution would be to
have the program provide a second parameter whenever one is not present
in the call to do_it(). In this way the program can pick whatever type
it likes that argument to be (in the case, the choice will be
function<Value>):

#include <iostream>

using std::cout;
using std::endl;

template< class Value>
struct function
{
void operator()(Value v)
{
cout << '*' << v << '*' << endl;
}
};

template< class Value, class Function>
void do_it( Value v, Function f )
{
cout << v << '\t';
f(v);
}

template< class Value>
void do_it( Value v)
{
do_it( v, function<Value>());
}

int main()
{
do_it(3);
}

With two do_it() overloads, calling do_it() with one argument forwards
the call to the other do_it(), providing its own the Function<Value>
object as the "missing", second parameter. Essentially, the program has
implemented C++'s default argument behavior for the do_it() function
all on its own.

Greg

Jan 13 '07 #3

P: n/a
On Jan 12, 10:01 pm, "Greg" <gre...@pacbell.netwrote:
krishnaros...@gmail.com wrote:
Hey all,
I've been running into a problem with default values to template'd
functions. I've boiled down my problem to this simple example code:
#include<iostream>
using namespace std;
// function object
template<typename Valuestruct function {
void operator()(Value v) {
cout << '*' << v << '*' << endl;
}
};
template<typename Value, typename Function>
void do_it(Value v, Function f = function<Value>()) {
cout << v << '\t';
f(v);
}
int main(int argc, char* argv[]) {
do_it(3);
return 0;
}
Basically, I want the second parameter of do_it to be anything that can
be called like a function with a single argument of type Value but I
want the default to be the default constructed object of type
function<Value>(). That's what I want, but what I get is this error:
test.cpp:19: error: no matching function for call to 'do_it(int)'

The problem is that there is no way to figure out the type of the
second type parameter in the "do_it(3)" function call. After all,
main() calls do_it() with only one argument, 3. So the compiler has to
figure out what type a second argument would have been - if main() had
called do_it() with two arguments (instead the one argument) that
main() passed in the actual call.

Of course, there is no way to ascertain the type of an absent argument
- nor can the program ever be able to answer that question.
That makes sense. And the compiler doesn't want to infer the 'default'
type from the default value of the second parameter?

Then in theory, I should be able to give a default type for the
Function template parameter and it should work? Or is there some reason
that wouldn't work as well? And why won't gcc let you give defaults to
template parameters for functions? Is there a fundamental reason it
doesn't allow this?
So another approach is needed to solve this problem. And that solution
would be to have the program provide a second parameter whenever one
is not present in the call to do_it(). In this way the program can pick whatever
type it likes that argument to be (in the case, the choice will be
function<Value>):

#include <iostream>

using std::cout;
using std::endl;

template< class Value>
struct function
{
void operator()(Value v)
{
cout << '*' << v << '*' << endl;
}
};

template< class Value, class Function>
void do_it( Value v, Function f )
{
cout << v << '\t';
f(v);
}

template< class Value>
void do_it( Value v)
{
do_it( v, function<Value>());
}

int main()
{
do_it(3);
}

With two do_it() overloads, calling do_it() with one argument forwards
the call to the other do_it(), providing its own the Function<Value>
object as the "missing", second parameter. Essentially, the program has
implemented C++'s default argument behavior for the do_it() function
all on its own.
Thanks a lot for your help and solution!

-krish

Jan 13 '07 #4

P: n/a
kr***********@gmail.com wrote:
That makes sense. And the compiler doesn't want to infer the 'default'
type from the default value of the second parameter?

Then in theory, I should be able to give a default type for the
Function template parameter and it should work? Or is there some reason
that wouldn't work as well? And why won't gcc let you give defaults to
template parameters for functions? Is there a fundamental reason it
doesn't allow this?
Default type parameters for function templates have been added to the
C++ working draft. So I would expect them to be part of the next C++
language Standard. So your solution should work in the future - just
probably not today - with your current C++ compiler.

Greg

Jan 13 '07 #5

P: n/a
kr***********@gmail.com wrote:
That makes sense. And the compiler doesn't want to infer the 'default'
type from the default value of the second parameter?

Then in theory, I should be able to give a default type for the
Function template parameter and it should work? Or is there some reason
that wouldn't work as well? And why won't gcc let you give defaults to
template parameters for functions? Is there a fundamental reason it
doesn't allow this?
Default type parameters for function templates have been added to the
C++ working draft. So I would expect them to be part of the next C++
language Standard. So your suggested solution (not the original
program) should be possible in the future - just probably not today -
with your current C++ compiler.

Greg

Jan 13 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.