开发者

VS2010 C++ member template function specialization error

开发者 https://www.devze.com 2023-01-07 03:26 出处:网络
I have the following (minimized) code, which worked in VC2005, but no longer works in 2010. template <typename TDataType>

I have the following (minimized) code, which worked in VC2005, but no longer works in 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; }
  };

It seems that when "index_type" is a dependent type, I always get a C2770: invalid explicit template argument(s) error on the specialization. Note that this code is actually enough to generate the error - an empty main is sufficient to com开发者_开发问答pile it, the template need not even be instantiated.

It works fine if index_type is not a dependent type. Any ideas why this is so in VC2010, if this is actually standard behaviour or a bug, and if I can work around it?


Workaround

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; } 

}; 


This is according to C++ Standard 14.7.3/18:

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well. <...>

It is not allowed to specialize Spec without specializing TDataType.

0

精彩评论

暂无评论...
验证码 换一张
取 消