【发布时间】:2015-08-05 03:05:53
【问题描述】:
如何获得一个布尔值来指示已知方法是否具有 const 限定符?
例如:
struct A {
void method() const {}
};
struct B {
void method() {}
};
bool testA = method_is_const<A::method>::value; // Should be true
bool testB = method_is_const<B::method>::value; // Should be false
在type_traits 标头中,我找到了可以使用的is_const 测试,但我需要方法类型,但我不确定如何获得它。
我试过了:std::is_const<decltype(&A::method)>::value,但它不起作用,我可以理解为什么(void (*ptr)() const) != const void (*ptr)())。
【问题讨论】:
-
解决最后一部分,因为第一个是返回void的常量函数指针,第二个是返回const void的函数指针。
-
另外,我将推测 is_const 正在捕获返回类型,而不是函数本身的 const 限定符。但是,我无法正确回答这个问题,因为我不确定如何让它按照您的意愿行事。
-
@William 感谢您的帮助。是的,我不知道如何检查 const 限定符 =/
标签: c++ templates c++11 typetraits