473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

defaults for template'd functions

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
5 1724
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: velthuijsen | last post by:
I've been reading (and using the examples in) Thinking in C++ (vol 2). One of the things that the author shows you can do with templates is the following: #include <iostream> using namespace...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
2
by: Drew McCormack | last post by:
I have a self written Tensor class which I need to write a number of elementwise operations for (eg sin, cos, abs, conj). I am trying to implement most of these in terms of standard library...
5
by: Mark Fox | last post by:
Hello, When you add a new web form in VS.NET it automatically adds the following namespaces at the top: using System; using System.Collections; using System.ComponentModel; using...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
by: vectorizor | last post by:
Hello all, I am attempting to vectorize few template functions with the Intel compiler, but without much success so far. Ok granted, this question is not 100% c++, but it is related enough that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.