473,699 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which specialization is selected?

[I just posted this to comp.lang.c++.m oderated, but I'm impatient]

Hi All,

The simple test program at the end of this message defines a class template
foo with a member function bar implemented by delegating to a static member
function of a specialization of the class template bar_impl, nested within foo.
The template bar_impl is partially specialized outside of foo. My question is
which specialization should be selected: the primary template or the partial
specialization. Or does the program exhibit undefined behavior?

Here are the results for several recent compilers:

* partial specialization is selected - VC7.1-8.0(beta), Comeau 4.3.3, Intel
8.0 for Windows

* primary template is selected: GCC 3.2-3.4.1

* fails to compile: CodeWarrior 8.3-9.2(eval), DigitalMars 8.38n

Best Regards,
Jonathan

----

#include <iostream>

template<typena me T>
struct foo {
template<typena me U, typename V>
struct bar_impl {
static void bar() { std:: cout << "unspecialized\ n"; }
};

void bar()
{
typedef bar_impl<foo<T> , int> type;
type::bar();
}
};

template<typena me T>
template<typena me V>
struct foo<T>::bar_imp l<foo<T>, V> {
static void bar() { std::cout << "specialized\n" ; }
};

int main()
{
foo<int> f;
f.bar();
}
Jul 22 '05 #1
2 1201
On Sun, 3 Oct 2004 20:22:16 -0600, "Jonathan Turkanis"
<te******@kanga roologic.com> wrote:
[I just posted this to comp.lang.c++.m oderated, but I'm impatient]

Hi All,

The simple test program at the end of this message defines a class template
foo with a member function bar implemented by delegating to a static member
function of a specialization of the class template bar_impl, nested within foo.
The template bar_impl is partially specialized outside of foo. My question is
which specialization should be selected: the primary template or the partial
specialization . Or does the program exhibit undefined behavior?

Here are the results for several recent compilers:

* partial specialization is selected - VC7.1-8.0(beta), Comeau 4.3.3, Intel
8.0 for Windows
I think they're right.
* primary template is selected: GCC 3.2-3.4.1

* fails to compile: CodeWarrior 8.3-9.2(eval), DigitalMars 8.38n

Best Regards,
Jonathan

----

#include <iostream>

template<typen ame T>
struct foo {
template<typena me U, typename V>
struct bar_impl {
static void bar() { std:: cout << "unspecialized\ n"; }
};

void bar()
{
typedef bar_impl<foo<T> , int> type;
type::bar();
The above doesn't cause an implicit instantiation, since T isn't known
- "type" is dependent.
}
};

template<typen ame T>
template<typen ame V>
struct foo<T>::bar_imp l<foo<T>, V> {
static void bar() { std::cout << "specialized\n" ; }
};

int main()
{
foo<int> f;
The above is the first use of the partial specialization that would
cause implicit instantiation to occur. The partial specialization has
been declared already.
f.bar();
}


By 14.5.4/1 it looks like it should compile and work as it does with
VC and EDG.

Tom
Jul 22 '05 #2

"Tom Widmer" <to********@hot mail.com> wrote in message
news:so******** *************** *********@4ax.c om...
On Sun, 3 Oct 2004 20:22:16 -0600, "Jonathan Turkanis"
<te******@kanga roologic.com> wrote:
[I just posted this to comp.lang.c++.m oderated, but I'm impatient]

Hi All,

The simple test program at the end of this message defines a class templatefoo with a member function bar implemented by delegating to a static member
function of a specialization of the class template bar_impl, nested within foo.The template bar_impl is partially specialized outside of foo. My question is
which specialization should be selected: the primary template or the partial
specialization . Or does the program exhibit undefined behavior?

Here are the results for several recent compilers:

* partial specialization is selected - VC7.1-8.0(beta), Comeau 4.3.3, Intel
8.0 for Windows


I think they're right.


Thanks. That's what I was hoping.

Jonathan
Jul 22 '05 #3

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

Similar topics

4
6478
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...
6
2635
by: jesse | last post by:
I am frustrated by class specialization. i don't think it helps me a lot. suppose we have template <class T> class Talkative { T& t; public:
8
7683
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
4
1962
by: TT \(Tom Tempelaere\) | last post by:
Comeau compiler complains (too few arguments for class template "B") at line *** #include <memory> template<typename T, size_t n> struct A {}; template<typename T, size_t n> struct B;
4
9200
by: Michael Sparks | last post by:
I started writing some code with the 2.0 beta compiler, and found out quickly that specialization is not supported with generics. For example, I want to do something like this: class number ... class integer : number ... class real : number ... class multiplier<_number> where _number : number ... class multiplier<_number> where _number : integer ...
2
1777
by: Yang Zhang | last post by:
I have a small program like this: //////////////////////////////////////////////// #include <iostream> using namespace std ; // set bits in an address template <typename T> inline T* set_addr(const T* addr, unsigned long n) {
2
290
by: Imre | last post by:
Hi I'd like to know what the problem is with the following code. I've tried compiling it with two compilers (VC++ 8.0, and Comeau online compiler), and both failed to compile it, saying that there's no B::G() function. It seems that the primary version of the CallFHelper template class is selected for both A and B, though I think that the specialization should be selected for B (after all, IntToVoid<sizeof(Dummy(&T::F))>::type _is_...
5
1645
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
6586
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
8704
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
9187
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...
1
8936
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
7777
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...
0
5879
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
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2361
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.