Is it possible to write an unclean template in C ++?

Is it possible to write an unclean template in C ++? That is, a template that sometimes gives a different result type or int for the same template parameters. For example, is it possible to write a template Foo<T> , where Foo<int>::type sometimes char , and in other cases is float ? Or the template Foo<T> , where Foo<double>::my_static_const_int sometimes 10 and the other 20?

+6
c ++ functional-programming templates purely-functional
source share
1 answer

It's impossible. If you have a template that behaves this way, it violates ODR and / or other rules, for example, that a specialization must be announced before it is created. Thus, you cannot just specify a specialization that will somehow change the typedef member to allow another type for all of the following references.

Remember that Foo<T> refers to a class if Foo is a class template. If a typedef member of a class is defined as one type at one point in the program, and another type at another point, then something must have gone wrong. Here are a few standard quotes that relate to this.


A specialization of a function template, a member function template, or a member function or a static data element of a class template can have multiple instantiation points within a translation unit. A class template specialization has at most one instantiation point within a translation unit. Specialization for any template can have instantiation points in several translation units. If two different points of instantiation give a template specialization of different values ​​in accordance with one definition rule (3.2), the program is poorly formed, diagnostics are not required.


If a template, a member template, or a member of a class template is explicitly specialized, then this specialization must be declared before the first use of this specialization, which will cause an implicit instantiation, in each translation unit in which such use is used; no diagnostics required.


(various "noise" is omitted)

[.. Various objects that can be defined somewhat throughout the program ..]. Given such an object named D, defined in more than one translation unit, then

  • each definition of D must consist of the same sequence of tokens;
  • in each definition of D, the corresponding names scanned in accordance with 3.4 refer to the entity defined in definition D, or must refer to the same object after resolving overload (13.3) and after agreeing on a partial template specialization (14.8.3). .
  • If D is a template and is defined in more than one translation unit, then the last four requirements from the list above apply to names from templates covering the scope used in the template definition (14.6.3), as well as to dependent names at the time of instantiation (14.6.2). If the definitions of D satisfy all these requirements, then the program should behave as if there was one definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.
+10
source share

All Articles