Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences of the interpretation of a non-dependent construct between definition context and point of instantiation in c++

N4527 14.6 [temp.res]/p8

If a hypothetical instantiation of a template immediately following its definition would be ill-formed due to a construct that does not depend on a template parameter, the program is ill-formed; no diagnostic is required. If the interpretation of such a construct in the hypothetical instantiation is different from the interpretation of the corresponding construct in any actual instantiation of the template, the program is ill-formed; no diagnostic is required. [ Note: This can happen in situations including the following:

(8.1) — a type used in a non-dependent name is incomplete at the point at which a template is defined but is complete at the point at which an instantiation is performed, or

(8.2) — an instantiation uses a default argument or default template argument that had not been defined at the point at which the template was defined, or

(8.3) — constant expression evaluation (5.20) within the template instantiation uses

(8.3.1) — the value of a const object of integral or unscoped enumeration type or

(8.3.2) — the value of a constexpr object or

(8.3.3) — the value of a reference or

(8.3.4) — the definition of a constexpr function,

and that entity was not defined when the template was defined, or

So, are these codes ill-formed?

code 1:

extern double b;

template<class T>
void f(T=b){}

void g(){
    f<double>();//ill-formed or not?
}

double b = 0;

void h(){
    f<double>();//ill-formed or not?
}

code 2:

//translation 1
extern double b;

template<class T>
void f(T=b){}

void g(){
    f<double>();//ill-formed or not?
}

//translation 2
double b = 0;

And Issue1850 Differences between definition context and point of instantiation

Various characteristics of entities referred to by a non-dependent reference in a template can change between the definition context and the point of instantiation of a specialization of that template. These include initialization (which affects whether an object can be used in a constant expression), function and template default arguments, and the completeness of types. There is implementation divergence as to whether these are checked in the definition context or at the point of instantiation. Presumably a rule is needed to make it ill-formed, no diagnostic required, if the validity of such a reference changes between the two contexts.

Can you show me more examples about how are characteristics of non-dependent names different between the two contexts? Typically about 8.2 and 8.3.1

like image 489
stackcpp Avatar asked Dec 08 '15 07:12

stackcpp


People also ask

What is the distinction between interpretation and construction?

Distinction between Interpretation and construction 1. Interpretation means the art of finding out the true sense of an enactment by giving the words their natural and ordinary meaning whereas Construction means drawing conclusions in the basis of the true spirit of the enactment. 2.

What is the difference between a construct and a variable?

Constructs can be conceptually defined in that they have meaning in theoretical terms. They can be abstract and do not necessarily need to be directly observable. Examples of constructs include intelligence or life satisfaction. Variables are created by developing the construct into a measurable form. Variables, by definition, correspond ...

What are the constructs in the methodology chapter?

One of the most tedious portions of the methodology chapter is describing the constructs, variables, and operational definitions. This section often confuses students because the three ideas seem to overlap. While these ideas are directly connected, they also have unique applications. Constructs are broad concepts or topics for a study.

What is a construct in psychology?

It’s a variable that’s usually not directly measurable. Psychologists develop and research constructs to understand individual and group differences. You can’t directly observe or measure these constructs. You need to investigate a collection of indicators to test hypotheses about the constructs. Constructs can range from simple to complex.


Video Answer


1 Answers

Here's an example:

extern const int b;

template<int, int>
void f(int);

template<int, const int &>
void f(long);

template<class>
void g() {
    f<0, b>(0);
}
// #1

extern const int b = 0;


int main(){
    g<int>(); 
}

// #2

A hypothetical instantiation at #1 will call void f<0, b>(long), because b isn't a constant expression at that point, so the (int) overload SFINAEs away. An instantiation at #2 (which is a point of instantiation of g<int>) will call void f<0, 0>(int), because by then b is a constant expression, the (int) overload is viable and wins overload resolution.

Clang and GCC will in fact call different fs with this code.

like image 152
T.C. Avatar answered Oct 04 '22 16:10

T.C.