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

谁说我不能喝 提交于 2019-12-01 05:50:39

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!