LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-07-2012, 02:37 PM   #1
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,046

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
C++ template function default type and value?


I want to define a function in which the type of one of the inputs is a template parameter (inferred if the input is given) but that input is also optional with a default value.

Is there any correct syntax for that in C++?

Incorrect syntax demonstrating what I want to do is:
Code:
struct nothing {};

template< class T >
int test_template(int x, T const& y = nothing()) {
    return x;
}

int test() {
    int i = test_template(1);
    int j = test_template(2,3);
    int k = test_template<nothing>(4);
    return i+j;
}
g++ complains about test_template(1) because it can't infer the type T. Other compilers are OK with that line but complain about test_template(2,3), which g++ likes but the other compilers complain the default value is incompatible with the inferred type, which is true but a really annoying check since the default value isn't used in the case where it isn't compatible. All the compiler are OK with test_template<nothing>(4) but that is only there for illustration. It isn't actually helpful.

Logically the code might be this instead, but no compiler likes this either:
Code:
struct nothing {};

template< class T = nothing >
int test_template(int x, T const& y = T()) {
    return x;
}

int test() {
    int i = test_template(1);
    int j = test_template(2,3);
    int k = test_template<nothing>(4);
    return i+j;
}
In the real use, the function replacing test has a lot of templated parameters before the one I want to default and does a lot of complicated operations on them and needs a compile time check (which I know how to code) on whether the defaulted parameter has type nothing to remove code that should only be present if that parameter was provided.

The best, but ugly, solution I can find is equivalent to:
Code:
struct nothing {};

template< class T>
int test_template(int x, T const& y) {
    return x;
}

int test_template(int x) {
    return test_template(x, nothing());
}

int test() {
    int i = test_template(1);
    int j = test_template(2,3);
    return i+j;
}
Anyone have any better ideas?
 
Old 04-07-2012, 03:22 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by johnsfine View Post
Logically the code might be this instead, but no compiler likes this either:
Code:
struct nothing {};

template< class T = nothing >
int test_template(int x, T const& y = T()) {
    return x;
}

int test() {
    int i = test_template(1);
    int j = test_template(2,3);
    int k = test_template<nothing>(4);
    return i+j;
}
Actually, if you compile the code above with g++, providing the -std=c++0x compiler option, then it does compile successfully.
 
Old 04-07-2012, 04:19 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by johnsfine View Post
The best, but ugly, solution I can find is equivalent to:
Code:
struct nothing {};

template< class T>
int test_template(int x, T const& y) {
    return x;
}

int test_template(int x) {
    return test_template(x, nothing());
}

int test() {
    int i = test_template(1);
    int j = test_template(2,3);
    return i+j;
}
This seems like the most logical solution, unless you want to count on having C++0x support (as in dwhitney67's suggestion.) If you're using templates as a preprocessor to control the code in the function, I'm sure you have other template code that's substantially uglier than a wrapper function.
Kevin Barry
 
Old 04-09-2012, 01:34 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
Quote:

Logically the code might be this instead, but no compiler likes this either:
Code:
struct nothing {};

template< class T = nothing >
int test_template(int x, T const& y = T()) {
    return x;
}

int test() {
    int i = test_template(1);
    int j = test_template(2,3);
    int k = test_template<nothing>(4);
    return i+j;
}
In non-0x C++ you still need <> on the calls to test_template<>() even if the actual types are inferred/defaulted.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make a template char* or wchar_t* default function argument binarybob0001 Programming 2 10-30-2009 01:46 AM
C++: different instances of a function template unihiekka Programming 3 02-05-2009 01:50 PM
c++ template function, expected constructor, destructor, or type conversion parv Programming 18 01-25-2008 02:54 PM
C++ Template For Generic Function - Is This Possible? taylor_venable Programming 6 06-05-2006 08:46 PM
function prototypes in template class' (c++) qwijibow Programming 4 12-13-2004 09:34 AM


All times are GMT -5. The time now is 08:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration