Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > defaults for template'd functions

Reply
Thread Tools

defaults for template'd functions

 
 
krishnaroskin@gmail.com
Guest
Posts: n/a
 
      01-13-2007
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

 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      01-13-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 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)'


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

 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      01-13-2007
(E-Mail Removed) 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 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)'


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

 
Reply With Quote
 
krishnaroskin@gmail.com
Guest
Posts: n/a
 
      01-13-2007
On Jan 12, 10:01 pm, "Greg" <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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 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)'

>
> 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

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      01-13-2007
(E-Mail Removed) 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

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      01-13-2007
(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FireFox search defaults Old Gringo Firefox 2 07-14-2005 01:50 PM
clear passwords/set defaults MT Cisco 1 03-11-2005 10:03 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Reset Cisco 1005 router to factory defaults? Noozer Cisco 1 04-28-2004 06:48 PM
Cisco 2950 Switch - Defaults Rafael Cisco 1 11-13-2003 03:53 PM



Advertisments