K
krishnaroskin
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 Value> struct 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
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 Value> struct 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