Jor*_*ans 2 c++ templates visual-studio-2010 specialization
我有以下(最小化)代码,它在VC2005中有效,但在2010年不再有效.
template <typename TDataType>
class TSpecWrapper
{
public:
typedef typename TDataType::parent_type index_type;
public:
template <bool THasTriangles>
void Spec(index_type& io_index)
{ std::cout << "False version" << std::endl; }
template <>
void Spec<true>(index_type& io_index)
{ std::cout << "True version" << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)
似乎当"index_type"是一个依赖类型时,我总是在特化上得到一个C2770:无效的显式模板参数错误.请注意,此代码实际上足以生成错误 - 空的main足以编译它,模板甚至不需要实例化.
如果index_type不是依赖类型,它可以正常工作.任何想法为什么在VC2010中如此,如果这实际上是标准行为或错误,并且我可以解决它?
解决方法
template <bool v> struct Bool2Type { static const bool value = v; };
template <typename TDataType>
class TSpecWrapper
{
public:
typedef typename TDataType::parent_type index_type;
public:
template <bool THasTriangles>
void Spec(index_type& io_index)
{
return SpecHelp(io_index, Bool2Type<THasTriangles>());
}
private:
void SpecHelp(index_type& io_index, Bool2Type<false>)
{ std::cout << "False version" << std::endl; }
void SpecHelp(index_type& io_index, Bool2Type<true>)
{ std::cout << "True version" << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1515 次 |
| 最近记录: |