explicit specialization od c'tor

G

Gernot Frisch

// variable creation object
template <class T> class CL
{
public:
CL (const T& t) {}
template <> CL<double>(const T& t) {}
};

How do I make a constructor for T = double, ..., so I can distinguish
between the types.

Thank you,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
P

Peter Koch Larsen

Gernot Frisch said:
// variable creation object
template <class T> class CL
{
public:
CL (const T& t) {}
template <> CL<double>(const T& t) {}
};

How do I make a constructor for T = double, ..., so I can distinguish
between the types.
Simply have the constructor untemplated:

template <class T> class CL
{
public:
CL (const T& t) {}
CL(double t) {}
};

/Peter
 
I

Ioannis Vranos

Peter said:
Simply have the constructor untemplated:

template <class T> class CL
{
public:
CL (const T& t) {}
CL(double t) {}
};


This does not work.
 
I

Ioannis Vranos

Gernot said:
// variable creation object
template <class T> class CL
{
public:
CL (const T& t) {}
template <> CL<double>(const T& t) {}
};

How do I make a constructor for T = double, ..., so I can distinguish
between the types.


I cannot think of another way...


template <>
class CL<double>
{
public:
CL (const double& t) {}
};
 
A

Andrey Tarasevich

Gernot said:
// variable creation object
template <class T> class CL
{
public:
CL (const T& t) {}
template <> CL<double>(const T& t) {}
};

How do I make a constructor for T = double, ..., so I can distinguish
between the types.
...

If you want to provide the explicit specialization for the constructor
alone you simply define it as follows

template<class T> class CL
{
CL(const T& t) {}
};

template<> CL<double>::CL(const double& t)
{
// Specialized implementation goes here
}
 
I

Ioannis Vranos

Andrey said:
If you want to provide the explicit specialization for the constructor
alone you simply define it as follows

template<class T> class CL
{
public:

CL(const T& t) {}
};

template<> CL<double>::CL(const double& t)
{
// Specialized implementation goes here
}


Pretty cool. How can we provide the declaration in this style?
 
V

Victor Bazarov

Ioannis said:
Pretty cool. How can we provide the declaration in this style?

What for? The original template definition already contains a declaration
anybody would need...
 
I

Ioannis Vranos

Ioannis said:
Pretty cool. How can we provide the declaration in this style?


I just noticed that this

template<class T> class CL
{
public:
CL(const T& t) {}
};


// Declaration
template<>
CL<double>::CL(const double& t);


template<>
CL<double>::CL(const double& t)
{
// Specialized implementation goes here
}


int main()
{
CL<double> a(2);
}


compiles.



Pretty weird style.
 
I

Ioannis Vranos

Victor said:
What for? The original template definition already contains a declaration
anybody would need...


For whatever reason (export may be? - or something else). :)



Anyway as I say in another message I just sent, probably I found the
declaration style. Pretty weird stuff.


I am not sure TC++PL mentions this style, or I have forgotten.
 
V

Victor Bazarov

Ioannis said:
For whatever reason (export may be? - or something else). :)



Anyway as I say in another message I just sent, probably I found the
declaration style. Pretty weird stuff.


I am not sure TC++PL mentions this style, or I have forgotten.

You can think of every function _definition_ as consisting of
a declaration and a body. So, if you already have a definition, just
replace the body with a semicolon :)
 
I

Ioannis Vranos

Victor said:
You can think of every function _definition_ as consisting of
a declaration and a body. So, if you already have a definition, just
replace the body with a semicolon :)


Yes that's what I thought. However it is the first time I see a
constructor definition (and declaration!) that is valid outside of the
class body and invalid in the inside.
 
A

Andrey Tarasevich

Ioannis said:
...

Yes that's what I thought. However it is the first time I see a
constructor definition (and declaration!) that is valid outside of the
class body and invalid in the inside.
...

It makes sense to me. This member specialization is only relevant to one
concrete specialization of the class template (the one with 'T ==
double'), not to the entire class template. It would be strange to see
it inside.

Now, if the constructor itself was a template

template<class T> class CL
{
public:
template<class U> CL(const U& t) {}
...
};

then it would be logical to allow declaring/defining it's
specializations inside the class definition

template<class T> class CL
{
public:
template<class U> CL(const U& t) {}

template<> CL(const double& t) { /* ... */ }
template<> CL(const int& t) { /* ... */ }
...
};

The above is only provided as a syntactical sketch. The latter code is
still illegal in C++ for a completely unrelated reason: member templates
cannot be explicitly specialized without explicit specialization of the
enclosing class template (come to think of it, I still don't know the
rationale for this limitation).
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top