我可以检查哪些函数模板已经或尚未实例化至少一次吗?

发布于 2024-12-28 02:59:03 字数 951 浏览 4 评论 0原文

我有很多模板代码。由于错误的模板代码除非经过编译,否则不会引发编译器错误,因此有什么方法可以检查编译器实际“编译”的模板函数以及完全忽略的模板函数吗?

编辑2:

如果特定的类模板函数模板对于任何参数类型实例化一次,那么就可以了。我想要从未以任何形式实例化的函数/类模板列表。

一个具体的例子如下。它们是两个不同的模板函数,我想知道其中一个或两个是否从未被实例化。

template <typename T_InputItr, typename T_Distance>
void advance( T_InputItr& aItr, T_Distance aN, bidirectional_iterator_tag )

template <typename T_InputItr, typename T_Distance>
void advance( T_InputItr& aItr, T_Distance aN, random_access_iterator_tag )

编辑:目前,对于类,我在 .cpp 文件中手动实例化它们,如下所示:

template TClass<int>;

对于我感兴趣的所有类型。这很好。但那是如果我记得这样做的话。有时我需要编写很多小的模板类/函数,但我忘记手动实例化一些函数/类模板,并在以后找到。我希望编译器告诉我这一点。

或者,如果我可以获得实例化的函数/类模板列表(对于任何参数),那么我可以将其与我可能在代码中 grep 的完整列表进行比较。

另一个好处是“测试”在模板类中编译了哪些方法,该模板类使用类型特征有选择地编译某些函数。在继续之前,我想确定我选择正确函数的逻辑是正确的。

I have a lot of template code. Since bad template code does not throw a compiler error unless it is compiled, is there any way I can check which template functions the compiler actually 'compiled' and which were ignored altogether?

EDIT 2:

If a particular class template or function template is instantiated once, for any parameter types, then that is OK. I want the list of function/class templates that were never instantiated in any form.

One particular example is the following. They are two distinct template functions, and I would like to know if either or both is never instantiated.

template <typename T_InputItr, typename T_Distance>
void advance( T_InputItr& aItr, T_Distance aN, bidirectional_iterator_tag )

template <typename T_InputItr, typename T_Distance>
void advance( T_InputItr& aItr, T_Distance aN, random_access_iterator_tag )

EDIT: Currently, for classes, I instantiate them in the .cpp file manually like this:

template TClass<int>;

for all the types I am interested in. That's well and good. But that is if I remember to do that. Sometimes I need to write a lot of small template classes/functions where I forget to instantiate some of the function/class templates manually and find out later down the road. I would like the compiler to tell me that.

Alternatively, if I could get the list of function/class templates that were instantiated (for any parameter), then I could compare that to the full list which I might grep for in the code.

Another benefit would be to 'test' which methods were compiled in a template class that uses type traits to selectively compile out certain functions. I want to be certain my logic for selecting the correct functions is correct before moving on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

扶醉桌前 2025-01-04 02:59:03

鉴于您使用的是 MSVC 2008,您可以通过生成链接器映射文件并搜索该函数的所有实例或通过 DIA 检查 .pdb 来完成此操作。您需要使用链接器标志 /OPT:NOICF 禁用 COMDAT 折叠,以便您可以找到恰好编译到同一程序集的函数。

Given that you are using MSVC 2008, you can do this by generating a linker map file and searching for all the instantiations of that function or inspecting the .pdb via DIA. You'll want to disable COMDAT folding with the linker flag /OPT:NOICF so that you can find functions that happen to compile to the same assembly.

少年亿悲伤 2025-01-04 02:59:03

有人提到“一切都可以通过添加间接级别来解决” - 您可以向每个函数添加静态断言并观察编译失败:

template <typename T>
struct Asserter
{
  static const bool value = false;
};

template <typename T>
struct Foo
{
  void foo()
  {
    static_assert(Asserter<T>::value, "Foo::foo() is being compiled.");
  }
  void bar()
  {
    static_assert(Asserter<T>::value, "Foo::bar() is being compiled.");
  }
};

int main()
{
  Foo<int> f;
  //f.foo();  // static assertion!
}

如果您不希望编译在每一步都中断,您可以发出 < a href="http://www.boost.org/libs/serialization/doc/static_warning.html" rel="nofollow">Boost 静态警告,或具有类似效果的东西。

Someone mentioned how "everything can be solved by adding a level of indirection" - you can add a static assert to each function and watch the compilation fail:

template <typename T>
struct Asserter
{
  static const bool value = false;
};

template <typename T>
struct Foo
{
  void foo()
  {
    static_assert(Asserter<T>::value, "Foo::foo() is being compiled.");
  }
  void bar()
  {
    static_assert(Asserter<T>::value, "Foo::bar() is being compiled.");
  }
};

int main()
{
  Foo<int> f;
  //f.foo();  // static assertion!
}

If you don't want compilation to break at each step, you can instead emit a Boost static warning, or something with a similar effect.

老旧海报 2025-01-04 02:59:03

编译后,您可以通过静态分析工具运行可执行文件,前提是您已设置编译器以包含正确的符号表……这将显示所有实例化的类及其模板参数。 这里是可用于静态代码分析的工具列表的链接

You can run your executable through a static analysis tool after compilation provided you have setup your compiler to include the proper symbol tables ... that will show all the instantiated classes along with their template arguments. Here is a link to a list of tools that can be used for static code analysis.

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