我对以下示例的实例化问题感到困惑:
#include <iostream>
void f(int){std::cout<<"int"<<std::endl;}//3
template <typename T>
void g(T t)
{
f(t);//4
}
void f(double){std::cout<<"double"<<std::endl;}
int main()
{
g<int>(1);//1.point of instantiation for g<int>
g<double>(1.1);//2.point of instantiation for g<double>, so f(double) is visible from here?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我虽然f是一个从属名称,1是g <int>的实例化点,2是g <double>的实例化点,所以对于g(1.1),f(double)是可见的,但是输出是
int
int
Run Code Online (Sandbox Code Playgroud)
如果我在3处评论f(int)的声明,gcc报告错误(不是惊讶),并指出f(t)在4是实例化点(惊讶!!).
test.cpp: In instantiation of ‘void g(T) [with T = int]’:
test.cpp:16:10: required from here
test.cpp:9:5: error: ‘f’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation …Run Code Online (Sandbox Code Playgroud) 特定
int a = 1;(00000000000000000000000000000001),
我做的只是
a=(a<<31)>>31;
Run Code Online (Sandbox Code Playgroud)
我认为a应该仍然1在这个陈述之后(我认为没有改变).但事实证明是-1(11111111111111111111111111111111).谁知道为什么?