我可以检查哪些功能模板已经或尚未被实例化至少一次吗? [英] Can I check which function templates have, or have not, been instantiated at least once?
问题描述
我有很多模板代码。因为坏的模板代码不会引发编译器错误,除非它被编译,是否有任何方式我可以检查哪个模板函数编译器实际上编译,一起被忽略?
编辑2:
如果特定的类模板或实例化一次,对于任何参数类型,那就OK了。我想要从未以任何形式实例化的函数/类模板的列表。
一个特殊的例子如下。它们是两个不同的模板函数,我想知道是否两个都从不实例化。
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> ;;
我感兴趣的所有类型。但是,如果我记得这样做。有时我需要写很多小的模板类/函数,我忘记了手动实例化一些函数/类模板,然后在路上找到。我想让编译器告诉我。
或者,如果我可以得到实例化的函数/类模板的列表(任何参数),那么我可以
的另一个好处是'测试'哪些方法是编译的一个使用类型traits选择性地编译某些函数的模板类。我想要确定我的逻辑是否正确选择正确的函数。有人提到如何一切都可以通过添加一个间接级别解决 - 你可以添加一个静态断言每个函数,看看编译失败:
template< typename T>
struct Asserter
{
static const bool value = false;
};
template< typename T>
struct Foo
{
void foo()
{
static_assert(Asserter< T> value,Foo :: foo()正在编译。 );
}
void bar()
{
static_assert(Asserter< T> value,Foo :: bar()
}
};
int main()
{
Foo< int> F;
//f.foo(); // static assertion!
}
如果不希望编译在每一步都断开,发出 Boost静态警告或具有类似效果的内容。 p>
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.
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.
这篇关于我可以检查哪些功能模板已经或尚未被实例化至少一次吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!