g++ 在哪里? (使用 -fno-implicit-templates)如果我没有显式实例化它们,可以从中获取模板定义吗?

发布于 2024-12-12 14:48:54 字数 599 浏览 3 评论 0原文

以下代码有效。

/* 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗梦空间 2024-12-19 14:48:55

该选项不会阻止所有模板实例化,只是阻止非内联实例化:

-fno-隐式模板
切勿为隐式实例化(即通过使用)的非内联模板发出代码;只发出显式实例化的代码。有关更多信息,请参阅模板实例化。

编译器内联代码中使用的所有 vector 成员函数,因此编译成功。

使用该选项编译时,以下内容将失败,因为它需要非内联实例化:

#include <vector>

int main()
{
    auto f = &std::vector<int>::resize;
}

That option doesn't prevent all template instantiations, just non-inline ones:

-fno-implicit-templates
Never emit code for non-inline templates which are instantiated implicitly (i.e. by use); only emit code for explicit instantiations. See Template Instantiation, for more information.

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:

#include <vector>

int main()
{
    auto f = &std::vector<int>::resize;
}
一人独醉 2024-12-19 14:48:55

根据手册页:

   -fno-implicit-templates
       Never emit code for non-inline templates which are instantiated implicitly (i.e. by use); only emit code for explicit instantiations.

现在,在您的示例中,您正在实例化类型,然后调用在类模板定义中定义的方法,因此隐式内联。

你可以试试这个:

template <typename T>
void noop( T const & ) {}
int main() {
   noop(1);
}

然后用 g++ -fno-implicit-templates -o test test.cpp 进行编译

According to the manpage:

   -fno-implicit-templates
       Never emit code for non-inline templates which are instantiated implicitly (i.e. by use); only emit code for explicit instantiations.

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:

template <typename T>
void noop( T const & ) {}
int main() {
   noop(1);
}

And then compile with g++ -fno-implicit-templates -o test test.cpp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文