Действительно ли возможно записать нечистый шаблон в C++?

Можно использовать команду "файла", если Вы на самом деле хотите узнать информацию о файле, а не полагаться на расширения.

, Если Вы чувствуете себя довольными использованием расширения, можно использовать grep, чтобы видеть, соответствует ли это.

6
задан Richard JP Le Guen 14 March 2013 в 03:15
поделиться

1 ответ

It's not possible. If you have a template that behaves that way, it violates the ODR and / or other rules, such as that a specialization should be declared before it would be instantiated. So you can't just put a specialization that would somehow change a typedef member to make it resolve to a different type for all following references.

Remember that Foo references a class if Foo is a class template. If the class's typedef member is defined to be one type at one point in the program, and another type at another point, then something must have been gone wrong. Here are various Standard quotes that concern this


A specialization for a function template, a member function template, or of a member function or static data member of a class template may have multiple points of instantiations within a translation unit. A specialization for a class template has at most one point of instantiation within a translation unit. A specialization for any template may have points of instantiation in multiple translation units. If two different points of instantiation give a template specialization different meanings according to the one definition rule (3.2), the program is ill-formed, no diagnostic required.


If a template, a member template or the member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.


(Various "noise" skipped)

[.. Различные объекты, которые могут быть определены множеством во всей программе ..]. Если такая сущность с именем D определена более чем в одной единице трансляции, то

  • каждое определение D должно состоять из одной и той же последовательности токенов;
  • в каждом определении D соответствующие имена, найденные в соответствии с 3.4, должны относиться к объекту, определенному в определении D, или должны относиться к тому же объекту, после разрешения перегрузки (13.3) и после сопоставления частичного шаблона специализация (14.8.3) ...
  • Если D является шаблоном и определен более чем в одной единице перевода, то последние четыре requirements from the list above shall apply to names from the template’s enclosing scope used in the template definition (14.6.3), and also to dependent names at the point of instantiation (14.6.2). If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.
10
ответ дан 10 December 2019 в 00:42
поделиться
Другие вопросы по тегам:

Похожие вопросы: