Home C++ template - several specializations - is behaviour defined?
Reply: 2

C++ template - several specializations - is behaviour defined?

Benjamin Barrois
1#
Benjamin Barrois Published in 2018-02-13 16:15:11Z

Let's imagine I declare a templated structure:

template <int N> struct mystruct;

Let's imagine my structure has a general behavior for all N, except when N == 0

I can declare for instance a special behavior for N == 0 with:

template<> struct mystruct<0> {int k = 0};

I can also declare the general behaviour with:

template<int N> struct mystruct {float k = 3.14;};

If I run in the main:

struct mystruct<-1> x;
struct mystruct<0> y;
struct mystruct<1> z;
std::cout << x.k << " " << y.k << " " << z.k << std::endl;

I get what I want : 3.14 0 3.14, whatever the order of template declarations.

However I am wondering if this behavior is insured. Indeed, when I declare:

struct mystruct<0> y;

In my mind, this could as well be defined by both mystruct implementations.

So the question is:

Does C++ normalization ensure the template specialization <0> to override the general basic implementation <int N>, whatever the templates specialization order in the code is?

StoryTeller
2#
StoryTeller Reply to 2018-02-13 16:36:57Z

For your particular case, because the specialization has to appear after the primary template, and assuming they both appear, then yes. Otherwise, if one TU sees the specialization, but another doesn't and they both use mystruct<0>, you'd get undefined behavior.

In the general case, with many specializations that may be quite complicated, the C++ standard has this to say at [temp.expl.spec]/7, emphasis mine:

The placement of explicit specialization declarations for function templates, class templates, variable templates, member functions of class templates, static data members of class templates, member classes of class templates, member enumerations of class templates, member class templates of class templates, member function templates of class templates, static data member templates of class templates, member functions of member templates of class templates, member functions of member templates of non-template classes, static data member templates of non-template classes, member function templates of member classes of class templates, etc., and the placement of partial specialization declarations of class templates, variable templates, member class templates of non-template classes, static data member templates of non-template classes, member class templates of class templates, etc., can affect whether a program is well-formed according to the relative positioning of the explicit specialization declarations and their points of instantiation in the translation unit as specified above and below. When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

R Sahu
3#
R Sahu Reply to 2018-02-13 16:34:32Z

Does C++ normalization ensure the template specialization <0> to override the general construction <N>, whatever the templates specialization order in the code is?

Just to clarify on terminology,

template<int N> struct mystruct {float k = 3.14;};

is not a specialization. It is the main/generic/base implementation.

The specialization mystruct<0> will be used whenever an mystruct<0> is used as long as the specialization is visible at the point mystruct<0> is used. That's the whole point of specialization.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299753 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO