473,385 Members | 1,814 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,385 software developers and data experts.

Default template arguments in function templates

Hi all,

I have the following two template function definitions:

template <typename Container, typename Sepatator>
string join(const Container& c, const Sepatator& s) {
// ...
}

template <typename Container>
string join(const Container& c) {
return join(c, ' ');
}

I tried to merge them into one using a default argument for Separator, but the compiler (gcc 3.3.1)
complains that default template arguments may not be used in function templates. What's the reason
for this limitation and, more importantly, is there a workaround for it ?

TIA,
George

Oct 4 '05 #1
1 4931
George Sakkis wrote:
I have the following two template function definitions:

template <typename Container, typename Sepatator>
string join(const Container& c, const Sepatator& s) {
// ...
}

template <typename Container>
string join(const Container& c) {
return join(c, ' ');
}

I tried to merge them into one using a default argument for Separator, but the compiler (gcc 3.3.1)
complains that default template arguments may not be used in function templates. What's the reason
for this limitation and, more importantly, is there a workaround for it ?


Ask in comp.std.c++ bout the reasoning behind prohibiting the default
template arguments for function templates, they discuss the actual
Standard document, they know the rationales for different parts of it
for sure. As to the work-around, you already have it: don't merge, keep
the two functions separate.

V
Oct 4 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
2
by: Michael Stembera | last post by:
I would like to use default parameters in nested templates but MS VC++ 7.1 chokes on it. Does anyone know how to fix the simple example below or if indeed it is possible? template <int N=7>...
1
by: Arne Petersen | last post by:
Hy, I've got a problem with member function templates compiled into libraries. I'm trying to get a library collection (coded for GNU gcc, where its compiled completly) being compiled on Visual...
11
by: BRG | last post by:
I know that default template arguments cannot be used in function templates but are default function parameters legal? That is, is this: ---------------------------------- #include <functional>...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
4
by: chrisstankevitz | last post by:
Apparently default template arguments can only be used in classes. I include two apps below. One works but is IMO messy. The other does not work and is IMO clean. Q1: Why would c++ dissallow...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
by: Chris Thomasson | last post by:
Is the following use of default template arguments Kosher? _______________________________________________________________ template< typename T, void (T::*T_fp_start) () = &T::start, void...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.