【发布时间】:2015-11-24 09:04:25
【问题描述】:
我有一段代码可以使用 icc 或 visual c++ 编译,但在我使用 gcc 或 clang 时却没有。
问题在于 gcc/clang 希望在 A<T>::bind(T& t) 或 callBindTo(T& t) 定义之前定义 bindTo(std::string& s, const int& i) 而不仅仅是在代码实例化之前。
我的问题是:为什么 Visual 和 icc 不需要它?哪个编译器在标准方面有正确的行为?
#include <string>
template <typename T>
class A
{
public:
static void bind(T& t);
};
template <typename T>
void A<T>::bind(T& t)
{
std::string s;
bindTo(s, t);
}
template <typename T>
void callBindTo(T& t)
{
std::string s;
bindTo(s, t);
}
void bindTo(std::string& s, const int& i)
{
s = i;
}
int main()
{
int i;
A<int>::bind(i);
callBindTo(i);
}
【问题讨论】:
-
这称为前向声明。函数声明/原型应始终在其使用之前。通常,所有函数都在源文件中包含的头文件中声明。
-
要搜索的词是两阶段查找。
-
感谢丢失的指针 ;) stackoverflow.com/questions/6273176/…
标签: c++ visual-c++ gcc clang icc