以下是一些代码:
#include<iostream>
using namespace std;
class ShowHello {
public:
string operator()(int x) {return "hello";}
};
template<typename F>
class A {
private:
static F f;
static inline string foo(const int& x) {
return f(x);
}
public:
A() {
cout << "foo: " << foo(10) << endl;
}
};
int main(int argc, char *argv[])
{
A<ShowHello> a;
return 0;
}下面是我通过一些优化来编译它:
$ g++ -std=c++11 -O1 -Wall test.cc -o test && ./test
foo: hello也没有优化
$ g++ -std=c++11 -O0 -Wall test.cc -o test && ./test
/tmp/ccXdtXVe.o: In function `A<ShowHello>::foo(int const&)':
test.cc:(.text._ZN1AI9ShowHelloE3fooERKi[_ZN1AI9ShowHelloE3fooERKi]+0x2a): undefined reference to `A<ShowHello>::f'
collect2: error: ld returned 1 exit status我做错了什么?
也是
$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.发布于 2017-10-25 14:48:38
您可以在这里声明一个静态成员变量f:
static F f;然后你在这里做odr-使用:
static inline string foo(const int& x) {
return f(x);
}这需要f的定义可用,但您还没有提供定义--只提供声明。这使得程序格式不正确,不需要诊断。然而,在实践中,只有当链接器实际需要定义时,定义才是必要的--在这种情况下,优化的构建只是将调用完全内联,甚至不需要使用静态成员或类。实际上,这些说明包括一些有趣的东西,如:
movl $1819043176, 16(%rsp) // this is "hell"
movl $5, %edx
movl std::cout, %edi
movq $5, 8(%rsp)
movb $111, 4(%rsi) // this is 'o'很酷的东西。
无论如何,未优化的构建实际上依赖于f的定义,因为调用是实际进行的--而且您没有定义,因此出现链接器错误。优化的构建不依赖于f,但无论如何您都应该定义它,以确保正确性。
template <typename F>
struct A { ... };
template <typename F>
F A<F>::f;https://stackoverflow.com/questions/46935282
复制相似问题