473,811 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function templates doesn't support default template parameters?

Compiling:

template <class T = int>
T foo(const T& t)
{}

int main(int argc, char *argv[])
{}

gcc complains:

,----
| /Users/william/repo/helloworlds/foo.cpp:2: error: default template
| arguments may not be used in function templates
`----

But I find in "TC++PL(3rd , special edition)" P.340, Bjarne is giving
function templates with default template parameters as examples.

If current standard doesn't support it, what is the reason here?

--
William
Jun 1 '08 #1
6 3679
On 2008-06-01 16:29, William Xu wrote:
Compiling:

template <class T = int>
T foo(const T& t)
{}

int main(int argc, char *argv[])
{}

gcc complains:

,----
| /Users/william/repo/helloworlds/foo.cpp:2: error: default template
| arguments may not be used in function templates
`----

But I find in "TC++PL(3rd , special edition)" P.340, Bjarne is giving
function templates with default template parameters as examples.

If current standard doesn't support it, what is the reason here?
It is not supported in the current standard (14.1 §9), I do not know why.

--
Erik Wikström
Jun 1 '08 #2
Erik Wikström wrote:
On 2008-06-01 16:29, William Xu wrote:
>Compiling:

template <class T = int>
T foo(const T& t)
{}

int main(int argc, char *argv[])
{}

gcc complains:

,----
| /Users/william/repo/helloworlds/foo.cpp:2: error: default template
| arguments may not be used in function templates
`----

But I find in "TC++PL(3rd , special edition)" P.340, Bjarne is giving
function templates with default template parameters as examples.

If current standard doesn't support it, what is the reason here?

It is not supported in the current standard (14.1 §9), I do not know why.
I wonder what difference one should expect from

template < typename T = int >
T foo ( T const & arg );

and

template < typename T >
T foo ( T const & arg );

How would you use the default type? The type T would be deduced from the
argument anyway, wouldn't it?
Best

Kai-Uwe Bux
Jun 1 '08 #3
Kai-Uwe Bux <jk********@gmx .netwrites:
How would you use the default type? The type T would be deduced from the
argument anyway, wouldn't it?
Consider the following example then. What if I want the default
comparision method to be Case_insensitiv e?

#include <iostream>

struct Case_insensitiv e
{
static bool eq(char c1, char c2) { return tolower(c1) == tolower(c2); }
};

struct Case_sensitive
{
static bool eq(char c1, char c2) { return c1 == c2; }
};

template <class Cmp>
bool eq(char c1, char c2)
{
return Cmp::eq(c1, c2);
}

int main(int argc, char *argv[])
{
char c1 = 'h', c2 = 'H';

/* These are okay. */
eq<Case_insensi tive>(c1, c2) ;
eq<Case_sensiti ve>(c1, c2);

/* But how about this one ? */
/* eq(c1, c2); */

}

--
William

http://williamxu.net9.org

You know what they say -- the sweetest word in the English language is revenge.
-- Peter Beard
Jun 1 '08 #4
William Xu wrote:
Kai-Uwe Bux <jk********@gmx .netwrites:
>How would you use the default type? The type T would be deduced from the
argument anyway, wouldn't it?

Consider the following example then. What if I want the default
comparision method to be Case_insensitiv e?

#include <iostream>

struct Case_insensitiv e
{
static bool eq(char c1, char c2) { return tolower(c1) == tolower(c2); }
};

struct Case_sensitive
{
static bool eq(char c1, char c2) { return c1 == c2; }
};

template <class Cmp>
bool eq(char c1, char c2)
{
return Cmp::eq(c1, c2);
}

int main(int argc, char *argv[])
{
char c1 = 'h', c2 = 'H';

/* These are okay. */
eq<Case_insensi tive>(c1, c2) ;
eq<Case_sensiti ve>(c1, c2);

/* But how about this one ? */
/* eq(c1, c2); */

}
Now, I can see your point.

On the other hand, it looks as though you want a default argument not a
default type:

#include <cctype>

bool case_insensitiv e ( char c1, char c2 ) {
return ( std::tolower(c1 ) == std::tolower(c2 ) );
}

bool case_sensitive ( char c1, char c2 ) {
return ( c1 == c2 );
}

typedef bool(* char_compare )(char,char);

bool eq ( char c1, char c2, char_compare comp = &case_sensit ive ) {
return ( comp( c1, c2 ) );
}

int main ( void ) {
eq( 'c', 'h' );
}
Then again, I am not at all sure whether it is a good idea to have a default
in this case. I generally do not like magic hiding somewhere. Sooner or
later it is going to bite you. From that point of view, I prefer the
verbose version

eq< case_sensitive >( c1, c2 );

to

eq( c1, c2 );

Best

Kai-Uwe Bux
Jun 1 '08 #5
On Jun 1, 4:46 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-06-01 16:29, William Xu wrote:
Compiling:
template <class T = int>
T foo(const T& t)
{}
int main(int argc, char *argv[])
{}
gcc complains:
But I find in "TC++PL(3rd , special edition)" P.340, Bjarne
is giving function templates with default template
parameters as examples.
That surprises me a bit (but someone walked off with my copy of
the 3rd edition, so I can't verify it).
If current standard doesn't support it, what is the reason
here?
It is not supported in the current standard (14.1 §9), I do
not know why.
Probably because originally, function template arguments could
only be deduced, not explicitly specified, and a defauld
argument doesn't make sense in that case. For that matter,
given the original poster's example, when would the default
argument be used?

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 1 '08 #6

"James Kanze" <ja*********@gm ail.coma écrit dans le message de news:
ff************* *************** **...legroup s.com...
On Jun 1, 4:46 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-06-01 16:29, William Xu wrote:
Compiling:
template <class T = int>
T foo(const T& t)
{}
int main(int argc, char *argv[])
{}
gcc complains:
But I find in "TC++PL(3rd , special edition)" P.340, Bjarne
is giving function templates with default template
parameters as examples.
That surprises me a bit (but someone walked off with my copy of
the 3rd edition, so I can't verify it).
If current standard doesn't support it, what is the reason
here?
It is not supported in the current standard (14.1 §9), I do
not know why.
Probably because originally, function template arguments could
only be deduced, not explicitly specified, and a defauld
argument doesn't make sense in that case. For that matter,
given the original poster's example, when would the default
argument be used?

--

From C++ Templates (Vandevoorde, Josuttis)

"When templates were originally added to the C++ language, explicit function
template arguments were not a valid construct. [...] Since then, however, it
is possible to specify explicitle function template arguments that cannot be
deduced. "

So the following should compile...

template <typename T1, typename T2 = int>
T2 count (T1 const& x);

Cause T2 cannot be deduced since it is the return parameter. However I tried
with intel c++ 9.1 and VS 2003 compilers and they give me an error...
--------
Eric Pruneau
Jun 2 '08 #7

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

Similar topics

2
4785
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> class A { };
31
3546
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will return. I tried to write something like this: template <class Type1, class Type2, class Type3> Type3 findMin(Type1 x, Type2 y){ return (x < y) ? x : y;
11
2002
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> template<class T, class C> bool count(T x, C cmp = std::less<T>()) { for(int i = 0; i < 9; ++i)
16
16298
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 ]
22
6033
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be given extern "C" linkage where it is to be assigned to a C function pointer. Ian
7
567
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
4
3105
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax) { T retVal = val; if ( retVal < vmin ) retVal = vmin;
7
2123
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor classes are derived from ProcessorBase, and are customized to handle BufferBase-derived objects. Each...
14
2205
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to the .cpp files. After some reading, I found that the only way to do this is to add the actual template instantiations in the .cpp file. But, how do you do that if you're not working with built-in types? For example, a template class might be,
8
284
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
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10403
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3868
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.