473,790 Members | 3,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function specialization

Hi,
today a guy came to #C++@IRCNet and during discusion he showed this piece of
code:

template<bool tswitch=false>

class clstmp

{

public:

int val;
inline clstmp()

{

ctor_clstmp<tsw itch>();

}


template<bool> inline void ctor_clstmp();

template<> inline void ctor_clstmp<fal se>()

{

val = 1;

}

template<> inline void ctor_clstmp<tru e>()

{

val = 2;

}

};

It does compile with VS.NET 7.1 compiler even with MS extensions disabled.
It doesn't compile with recent GCC 3.5 fresh from CVS and neither with
Comeau's web test drive compiler. Now I would like to hear ruling on whether
it is valid C++ code or not.

V.H.


Jul 22 '05 #1
5 1410
"Vaclav Haisman" <V.*******@sh.c vut.cz> wrote in message
news:cf******** ***@ns.felk.cvu t.cz
Hi,
today a guy came to #C++@IRCNet and during discusion he showed this
piece of code:

template<bool tswitch=false>

class clstmp

{

public:

int val;
inline clstmp()

{

ctor_clstmp<tsw itch>();

}


template<bool> inline void ctor_clstmp();

template<> inline void ctor_clstmp<fal se>()

{

val = 1;

}

template<> inline void ctor_clstmp<tru e>()

{

val = 2;

}

};

It does compile with VS.NET 7.1 compiler even with MS extensions
disabled. It doesn't compile with recent GCC 3.5 fresh from CVS and
neither with Comeau's web test drive compiler. Now I would like to
hear ruling on whether it is valid C++ code or not.

V.H.

According to Vandevoorde and Josuttis (C++ Templates: The Complete Guide, p.
199), you can only specialise member templates if the enclosing class is
also fully specialised. Your sample code does not fully specialise the
enclosing class, so is not legal.

The following is an example of legal code (Comeau compiles it):

template<bool tswitch=false>
class clstmp
{
public:
int val;
inline clstmp()
{
ctor_clstmp<tsw itch>();
}

template<bool>
inline void ctor_clstmp();
};

template<>
template<>
inline void clstmp<true>::c tor_clstmp<fals e>()
{
val = 1;
}

template<>
template<>
inline void clstmp<true>::c tor_clstmp<true >()
{
val = 2;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
John Carson wrote in news:41******@u senet.per.parad ox.net.au in
comp.lang.c++:

According to Vandevoorde and Josuttis (C++ Templates: The Complete
Guide, p. 199), you can only specialise member templates if the
enclosing class is also fully specialised. Your sample code does not
fully specialise the enclosing class, so is not legal.


Does anybody know where this is stated in the *Standard* ?

I just spent over an hour looking, and didn't find it.

TIA.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
news:Xn******** *************** ***********@130 .133.1.4...
John Carson wrote in news:41******@u senet.per.parad ox.net.au in
comp.lang.c++:

According to Vandevoorde and Josuttis (C++ Templates: The Complete
Guide, p. 199), you can only specialise member templates if the
enclosing class is also fully specialised. Your sample code does not
fully specialise the enclosing class, so is not legal.


Does anybody know where this is stated in the *Standard* ?

I just spent over an hour looking, and didn't find it.


14.7.3/2 "An explicit specialization shall be declared in the namespace of
which the template is a member, or, for member templates, in the namespace
of which the enclosing class or enclosing class template is a member. "

-Sharad
Jul 22 '05 #4
Sharad Kala wrote in news:2o******** ****@uni-berlin.de in comp.lang.c++:

"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
news:Xn******** *************** ***********@130 .133.1.4...
John Carson wrote in news:41******@u senet.per.parad ox.net.au in
comp.lang.c++:
>
> According to Vandevoorde and Josuttis (C++ Templates: The Complete
> Guide, p. 199), you can only specialise member templates if the
> enclosing class is also fully specialised. Your sample code does
> not fully specialise the enclosing class, so is not legal.
>
>


Does anybody know where this is stated in the *Standard* ?

I just spent over an hour looking, and didn't find it.


14.7.3/2 "An explicit specialization shall be declared in the
namespace of which the template is a member, or, for member templates,
in the namespace of which the enclosing class or enclosing class
template is a member. "


Thanks, for some reason I got 14.7 and read "14.7 Template instantiation"
and missed the "... and specialization" :).

But 14.7.3/2 is only part of the OP's problem, the quote above from
"C++ Templates: The Complete Guide" also states that the *enclosing*
class template is fully specialized.

However you inspired me to keep reading and that bit is in 14.7.3/18.

Thanks again.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
"Vaclav Haisman" <V.*******@sh.c vut.cz> wrote:
[code reformatted] template<bool tswitch=false>
class clstmp
{
public:
int val;

inline clstmp() {
ctor_clstmp<tsw itch>();
Why doesn't it complain that "ctor_clstm p" is an unrecognized symbol?
or is it OK as long as it has been declared at the point where an
object of this class type is created?

In fact my compiler allows:
inline clstmp() {
fucq ctor_clstmp<tsw itch>();
jlk132j4l1k-11-;sa;f{~~~~}%%*) (*&
}
}

template<bool> inline void ctor_clstmp();

template<> inline void ctor_clstmp<fal se>() {
val = 1;
}
template<> inline void ctor_clstmp<tru e>() {
val = 2;
}
};


I get the delightful error message:
E2490 Specialization within template classes not yet implemented
Jul 22 '05 #6

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

Similar topics

4
6490
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
7
5076
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone offered me a kind explanation. TIA Function template and specialization below works fine: template <typename T> T maximum (T a, T b) {
2
5780
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
1
1651
by: SainTiss | last post by:
Hi, I've been looking into the standard for a clear statement on whether partial specialization of member functions of class templates is allowed or not. 14.7.3/4 says that explicit specialization of a member function is legal, but doesn't state that partial specialization is not. One might argue that the standard indicates that partial specialization implies a distinct template, and therefore defining a member function
1
2464
by: Rafal Dabrowa | last post by:
Consider the following code: template <class T> void f(T, int) {} template <class T> void f(int, T) {} template<> void f(int, int) {} // ambiguous ? My compiler complains that the last definition is an "ambiguous template specialization `f<>' for `void f(int, int)' ". I have tested this on a
5
6589
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
3690
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return false;
6
2767
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave no errors compiling this:
5
1653
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
13
6599
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10419
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
10201
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
10147
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
9987
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...
0
9023
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7531
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
6770
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();...
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.