这似乎是MSVC的错误。使用表达式T()
(这是显式类型转换,就标准而言)将导致指定类型的前值。
表达T()
,其中T
是简单型说明符或用于非阵列的完整对象类型或(可能CV修饰)void
型类型名说明符,创建的prvalue指定的类型,这是值初始化
只有与该const
将被忽略非类类型,由于非类prvalues不能有CV-限定类型的规则:
类prvalues可以有cv限定类型;非职业价值总是有cv不合格的类型。
因此,通过T()
这里创建的临时对象应该是const
,因此应该调用const
成员函数。我们可以看看the proposal中包含的原因。它指出,add_const
,add_volatile
,add_cv
,add_pointer
,add_reference
类型特征已从提案中删除,但在Boost用户投诉后恢复。
基本原理是,这些模板都用作编译时函数,它将一种类型转换为另一种类型[...]
给出的例子是:
// transforms 'tuple<T1,T2,..,Tn>'
// to 'tuple<T1 const&,T2 const&,..,Tn const&>'
template< typename Tuple >
struct tuple_of_refs
{
// transform tuple element types
typedef typename mpl::transform<
typename Tuple::elements,
add_reference< add_const<_1> > // here!
>::type refs;
typedef typename tuple_from_sequence<refs>::type type;
};
template< typename Tuple >
typename tuple_of_refs<Tuple>::type
tuple_ref(Tuple const& t)
{
return typename tuple_of_refs<Tuple>::type(t);
}
你能想到的mpl::transform
为采取的编译时元编程相当于一个函数指针作为其第二个模板参数 - add_reference<add_const<...>>
被施加到每种类型的在Tuple::elements
。这根本不能用const
表示。
最有可能的错误,按预期在gcc中工作... – Nim 2013-02-28 12:46:41