检查在编译的时候类的构造函数签名 [英] Check at compile time class constructor signature
问题描述
有没有一种方法来检查的,如果一些类有某些参数的构造函数编译时间?
?
Is there a way to check at compile time if some class has constructor with certain arguments ? ?
例如:
class foo {
foo(std::string &s) {
}
};
我想在编译时检查与标准::字符串和放大器,构造; 总是确定。也许提升提供了这样的功能?
I want to check at compile time that constructor with std::string& always defined. Maybe boost provides such functionality ?
推荐答案
的常用方法,以检查是否存在特定的功能是把它的地址并将其分配给一个虚拟变量。这是一个很大的precise比迄今为止所提到的试验,因为这将验证功能确切的签名。而问题是,特别是关于 字符串和放大器;
的签名,所以非const,因此presumably修改字符串。
The common way to check if a specific function exists is to take its address and assign it to a dummy variable. This is a lot more precise than the tests mentioned so far, because this verifies the exact function signature. And the question was specifically about string&
in the signature, so non-const and thus presumably modifying the string.
然而,在这种情况下,你不能使用起飞的地址和指派,它绝招:构造函数没有地址。那么,你如何检查签名呢?简单:在一个虚拟类交好了。
However, in this case you cannot use the take-the-address-and-assign-it trick: constructors don't have addresses. So, how do you check the signature then? Simply: Befriend it in a dummy class.
template<typename T>
class checkSignature_StringRef {
friend T::T(string&);
};
这也是一个非常特殊检查:它甚至不会匹配诸如相似的构造:: foo的美孚(标准::字符串&放大器; S,INT假= 0)
This too is a very specific check: it will not even match similar constructors like foo::foo(std::string &s, int dummy = 0)
.
这篇关于检查在编译的时候类的构造函数签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!