g++ 在哪里? (使用 -fno-implicit-templates)如果我没有显式实例化它们,可以从中获取模板定义吗?
以下代码有效。
/* hello.cc */
#include <iostream>
#include <vector>
void vec_print()
{
std::vector<int> is(10, 1);
for (size_t i = 0; i < is.size(); ++i)
std::cout << is[i] << " ";
std::cout << std::endl;
}
/* main.cc */
void vec_print();
int main()
{
vec_print();
}
当我遵守这个规定时,
g++ -fno-implicit-templates -Wall -Wextra -c hello.cc
g++ -fno-implicit-templates -Wall -Wextra -c main.cc
g++ hello.o main.o -o hello
我没有收到任何警告,并且它编译并运行良好。我的印象是这不应该发生。我正在使用 gcc 4.4.5。
The following code works.
/* hello.cc */
#include <iostream>
#include <vector>
void vec_print()
{
std::vector<int> is(10, 1);
for (size_t i = 0; i < is.size(); ++i)
std::cout << is[i] << " ";
std::cout << std::endl;
}
/* main.cc */
void vec_print();
int main()
{
vec_print();
}
When i complie this with
g++ -fno-implicit-templates -Wall -Wextra -c hello.cc
g++ -fno-implicit-templates -Wall -Wextra -c main.cc
g++ hello.o main.o -o hello
I get no warnings and it compiles and runs fine. I am under the impression this shouldn't happen. I am using gcc 4.4.5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该选项不会阻止所有模板实例化,只是阻止非内联实例化:
编译器内联代码中使用的所有
vector
成员函数,因此编译成功。使用该选项编译时,以下内容将失败,因为它需要非内联实例化:
That option doesn't prevent all template instantiations, just non-inline ones:
The compiler inlines all the
vector
member functions used in your code, so compilation succeeds.The following will fail when compiled with that option, since it requires a non-inline instantiation:
根据手册页:
现在,在您的示例中,您正在实例化类型,然后调用在类模板定义中定义的方法,因此隐式内联。
你可以试试这个:
然后用 g++ -fno-implicit-templates -o test test.cpp 进行编译
According to the manpage:
Now in your example you are instantiating the type, and then calling methods that are defined inside the class template definition and are thus implicitly
inline
.You can try this:
And then compile with g++ -fno-implicit-templates -o test test.cpp