Access primary template members?

  • Thread starter Johannes Schaub (litb)
  • Start date
J

Johannes Schaub (litb)

Here is my question, which i have not found an answer to yet:

template<typename A, typename B>
struct A { typedef void type; };

template<typename A, typename B>
struct A<B, A> { };

Now, how can we access the "A<a, b>::type" of the primary template? Is there
any way to inhibit using the partial specialization? Maybe with some uber
trickery?
 
F

Francesco S. Carta

on 14/08/2010 12:48:08 said:
Here is my question, which i have not found an answer to yet:

template<typename A, typename B>
struct A { typedef void type; };

template<typename A, typename B>
struct A<B, A> { };

Now, how can we access the "A<a, b>::type" of the primary template? Is there
any way to inhibit using the partial specialization? Maybe with some uber
trickery?

I'm not so sure I understand what you're after.

First of all, your code does not compile on my MinGW 4.4.0, and I think
it is correct as the name A in the first template is already a template
parameter name and cannot be used as name of the template class itself -
I've changed the template struct name and it compiled (leaving the type
parameters unchanged).

After that, I'm not able to understand what the specialization should be
doing by swapping the types... the compiler accepts it but I really
cannot see how it could ever distinguish the two when I'd be
instantiating them.

I think that a template specialization is something like an overloading,
which should completely redefine the internal stuff and doesn't inherit
nothing from the more general template (apart from the number of type
parameters), hence I'd say that there is no way to "see" any part of the
general template from its specialization.

I didn't look up anything on the standard to verify what I just said,
this is just based on my current understanding of them.
 
F

Francesco S. Carta

I'm not so sure I understand what you're after.

First of all, your code does not compile on my MinGW 4.4.0, and I think
it is correct as the name A in the first template is already a template
parameter name and cannot be used as name of the template class itself -
I've changed the template struct name and it compiled (leaving the type
parameters unchanged).

After that, I'm not able to understand what the specialization should be
doing by swapping the types... the compiler accepts it but I really
cannot see how it could ever distinguish the two when I'd be
instantiating them.

I think that a template specialization is something like an overloading,
which should completely redefine the internal stuff and doesn't inherit
nothing from the more general template (apart from the number of type
parameters), hence I'd say that there is no way to "see" any part of the
general template from its specialization.

Seems I was wrong, the original template can still be seen if it gets
explicit instantiated before defining the specialization.

That changes completely the template resolution and all further
instantiations, have a look at this:

//-------
#include <iostream>
#include <string>

using namespace std;

// (ORIG) primary template

template<typename A, typename B>
struct Struct {
void whoami() {
cout << "General version" << endl;
A a = string("string");
B b = 42;
cout << "a=" << a << " b=" << b << endl;
cout << "--------------\n" << endl;
}
};

// (ORIG INST) explicit instantiation

template class Struct<string, int>;

// (SPEC) template specialization

template<typename A, typename B>
struct Struct<B, A> {
void whoami() {
cout << "Specialized version calling" << endl;
Struct<A, B> s;
s.whoami();

// (CHECK) ensure type A can be constructed from a string

A a = string("");
}
};

int main() {

// (USE ORIG)

Struct<string, int> orig;

// The following compiles and uses (ORIG)
// only because (ORIG INST) is present.

// Removing (ORIG INT), then (SPEC) gets used
// and the compilation fails during (CHECK)

orig.whoami();

// (USE SPEC)

Struct<int, string> spec;
spec.whoami();

return 0;
}

/*
output:

General version
a=string b=42
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top