KTC*_*KTC 3 c++ templates typedef visual-studio
我正在看一些大量使用模板的代码.它在GCC上编译得很好,但在VS(在2003 - 2010 beta 1上测试)上没有编译,在语法分析期间它失败了.不幸的是,我不知道足够的代码结构能够减少问题并只在几行中重现它,所以我只能猜测原因.我希望有人能指出我正确的方向.
我们有
template< class UInt, typename IntT,
bool is_signed = std::numeric_limits<IntT>::is_signed >
struct uii_ops_impl;
// ....
template<class UInt>
struct uii_ops_impl< UInt,
typename make_signed<typename UInt::digit_type>::type, true >
{
typedef UInt unbounded_int_type;
typedef typename make_signed< typename unbounded_int_type::digit_type >::type
integral_type;
// ...
static void add(unbounded_int_type& lhs, integral_type rhs);
// ...
};
template<class UInt>
void uii_ops_impl<
UInt, typename make_signed<typename UInt::digit_type>::type,
true >::add(unbounded_int_type& lhs, integral_type rhs)
{
// ....
}
Run Code Online (Sandbox Code Playgroud)
在VS上编译时,它返回的第一条错误消息(在许多中)是
:错误C2065:'
unbounded_int_type':未声明的标识符
我的意思是,指向typedef吧?:-S
编辑:
这似乎与某些事情有关
typename make_signed<typename UInt::digit_type>::type
Run Code Online (Sandbox Code Playgroud)
用作模板参数.在其余的代码中,在member function参数中使用的类似typedef编译得很好.到目前为止,我能看到的唯一区别是没有其他情况将上述行作为模板参数.make_signed来自Boost.TypeTraits.
编辑:
好吧,也许那不是它,因为完全相同的事情是在另一个编译好的文件中完成的.嗯...
赏金编辑:
好吧,我认为现在很明显问题实际上并不是编译器抱怨的地方.只有该特定点的两个成员函数定义失败.事实证明,显式限定参数仍然无法编译.唯一的直接解决方案是定义内联函数.这通过了语法分析.但是,当尝试安装模板时,VS现在失败,因为std::allocator<void>没有size_type成员.原来VS有一个特殊的std::allocator<T>T = void,没有声明一个size_type.我认为size_type是所有分配器的必需成员?
所以现在的问题是,在语法分析期间可能会对VS造成太大的影响,因为它抱怨完全不相关的非问题是错误,你如何调试这些代码?
对于那些有太多时间闲暇的人,我试图在VS中工作的代码是Kevin Sopp在Boost沙箱中的mp_math,它基于libtommath.
我认为这可能是由一些情况造成的
unbounded_int_type是一种non-dependent类型(定义于14.6.2.1)我怀疑Visual C++无法进行此查找,而是出错.正如其他人提到的那样,您可以在成员函数定义中明确限定类型名称.然后依赖于类型,这将触发编译器的机制以延迟名称查找直到实例化.
template<class UInt>
void uii_ops_impl<
UInt, typename make_signed<typename UInt::digit_type>::type,
true >::add(typename
/* repeat-that-beast uii_ops...true> here */
::unbounded_int_type& lhs,
typename /* and here too */::integral_type rhs)
{
// ....
}
Run Code Online (Sandbox Code Playgroud)