decltype,result_of或typeof? [英] decltype, result_of, or typeof?
问题描述
我有:
class A {
public:
B toCPD
和:
template< typename T>
class Ev {
public:
typedef result_of(T :: toCPD())D;
实例化 Ev< A>
后,编译器说:
meta.h:12:错误:'T :: toCPD'不是类型
由于你获得的结果取决于模板参数,
decltype
是标准的C ++ 11功能。它是一个接受表达式并返回类型的操作符。
typedef typename decltype(T )D; //不能使用T ::作为非静态
如果 T )不是有效的(
T
不是默认可构造的),您将需要 declval
这是一个接受一个类型并返回该类型的无意义无效值的函数。 declval
只能用于未评估的上下文,例如 decltype
。
typedef typename decltype(std :: declval< T>()。toCPD())D;
在C ++ 11之前, decltype
Microsoft的MSVC编译器的非标准扩展。
typeof
是GCC等效的前C ++ 11扩展,如 decltype
,它也被克隆在其他编译器中。 此处是其来自GCC的文档。该页面不提供功能之间的比较,但它指出,当使用标准模式时, typeof
必须调用 __ typeof __
-std = c ++ YY
,你应该总是),它可以在C和C ++。
为了C兼容性, __ typeof __
不会从glvalue表达式解析引用类型。所以,它真的只适用于C.这可能解释了为什么C ++功能没有继承更自我解释的名称:GNU不愿意牺牲向后兼容性,而Microsoft不太在乎C,也许需要更少的更改。
result_of
是一个C ++ 11元函数TR1库从2006)。它是一个模板,它接受一个可调用的类型(如函数 int(void)
,函数指针 int(*)(void)
,函子类实现 operator()
或指向成员函数& T :: toCPD $ c $
要使用 result_of
this
的替代。 typedef typename std :: result_of< decltype(& T :: toCPD)(T *)> :: type D;
这很脆弱,因为& T :: toCPD
无法解决,如果有任何重载,如非const版本。这是真的,尽管事实上必须明确写出 T *
或 T const *
!在大多数情况下,你最好使用 decltype
和 declval
。
I have:
class A {
public:
B toCPD() const;
And:
template<typename T>
class Ev {
public:
typedef result_of(T::toCPD()) D;
After instantiating Ev<A>
, the compiler says:
meta.h:12: error: 'T::toCPD' is not a type
neither decltype nor typeof work either.
Since whatever result you obtain depends on the template parameter, typedef typename
is necessary.
decltype
is a standard C++11 feature. It is an "operator" which takes an expression and returns a type.
typedef typename decltype( T().toCPD() ) D; // can't use T:: as it's nonstatic
If T()
isn't a valid (T
not default-constructible) you will want declval
which is a function that takes a type and returns a meaningless, invalid value of that type. declval
can only be used in unevaluated contexts such as decltype
.
typedef typename decltype( std::declval<T>().toCPD() ) D;
Before C++11, decltype
was a non-standard extension by Microsoft's MSVC compiler. Its behavior might have been changed slightly by standardization.
typeof
is GCC's equivalent pre-C++11 extension like decltype
, which was also cloned in other compilers. Here is its documentation from GCC. That page provides no comparison between the features, but it notes that typeof
must be called __typeof__
when using a standard mode (-std=c++YY
, which you should always do), and it is available in C as well as C++.
For the sake of C compatibility, __typeof__
will not resolve a reference type from a glvalue expression. So, it's really only suitable for C. This probably explains why the C++ feature didn't inherit the more self-explanatory name: GNU was unwilling to sacrifice backward compatibility, whereas Microsoft cares less about C and perhaps needed fewer changes.
result_of
is a C++11 metafunction (previously standardized in the ISO TR1 library from 2006). It is a template which takes a callable type (such as a function int(void)
, function pointer int(*)(void)
, functor class implementing operator()
, or pointer-to-member-function &T::toCPD
) and an argument type-list for that type, and provides the return type if the call would work.
To use result_of
with a pointer to member function, you must include the parent object type in the argument list as a surrogate for this
.
typedef typename std::result_of< decltype( & T::toCPD ) ( T * ) >::type D;
This is very brittle, though, because &T::toCPD
cannot be resolved if there's any overloading, such as a non-const version. This is true despite the fact that T *
or T const *
must be explicitly written out! In most cases, you're better off with decltype
and declval
.
这篇关于decltype,result_of或typeof?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!