Which specialization is selected?

  • Thread starter Jonathan Turkanis
  • Start date
J

Jonathan Turkanis

[I just posted this to comp.lang.c++.moderated, 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<typename T>
struct foo {
template<typename 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<typename T>
template<typename V>
struct foo<T>::bar_impl<foo<T>, V> {
static void bar() { std::cout << "specialized\n"; }
};

int main()
{
foo<int> f;
f.bar();
}
 
T

Tom Widmer

[I just posted this to comp.lang.c++.moderated, 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<typename T>
struct foo {
template<typename 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<typename T>
template<typename V>
struct foo<T>::bar_impl<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
 
J

Jonathan Turkanis

Tom Widmer said:
[I just posted this to comp.lang.c++.moderated, 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.

Thanks. That's what I was hoping.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top