c ++ 11 -Template MetaProgramming - 错误:模板参数未在部分特化中使用

时间:2016-02-26 17:20:54

标签: c++ c++11 template-meta-programming

我遇到了部分模板实现,我的想法是提供一个使用枚举类定义在编译时选择的类(构建器),同时我想提供文件名和类名从工厂单身人士管理。但这不是编译,我正在尝试几个小时,但我看不出我做错了什么。 这是代码:

enum class BuildersType
{
   ComunicationBuilder
};

//class definition
template<BuildersType, class ... Args>
class BuiderType;

//class implementation
template<const char * const FILENAME , const char *  const CLASSNAME, class ClassToConfigurate>
class BuiderType<BuildersType::ComunicationBuilder,const char * const ,const char * const ,ClassToConfigurate>
{
   public:

};

template<const char * const FILENAME , const char * const CLASSNAME>
class AnotherBuilder
{
};

namespace Test
{
   static constexpr char FILENAME []="aFileName";
   static constexpr char  CLASSNAME []="ClassName";
   class TestClass{};
}

int main()
{

   BuiderType<BuildersType::ComunicationBuilder,Test::FILENAME,Test::CLASSNAME,Test::TestClass> aBuilder;
   AnotherBuilder<Test::FILENAME,Test::CLASSNAME> aAnotherBuilder;
   return 0;
}

编译输出:

Error: template parameters not used in partial specialization:
 class BuiderType<BuildersType::ComunicationBuilder,const char * const ,const char * const ,ClassToConfigurate>
       ^
main.cpp:14:7: error:         'FILENAME'
main.cpp:14:7: error:         'CLASSNAME'

在这个时候,我真的很累,我正在寻求帮助。 Thx提前 // ================================================ ===== 为简单起见,我将使用解决方案发布代码:

enum class BuildersType
{
  ComunicationBuilder
};

//class definition
//Add here definition here of the templates non type arguments
template<BuildersType, const char * const FILENAME , const char *  const CLASSNAME,class ... Args>
class BuiderType;

//class implementation

template<const char * const FILENAME , const char *  const CLASSNAME, class ClassToConfigurate, class ... Args>
class BuiderType<BuildersType::ComunicationBuilder,FILENAME , CLASSNAME ,ClassToConfigurate,Args...>
{
public:

};
template<const char * const FILENAME , const char * const CLASSNAME>
class AnotherBuilder
{

};
namespace Test
{
static constexpr char FILENAME []="aFileName";
static constexpr char  CLASSNAME []="ClassName";
    class TestClass{};
}
int main()
{

    BuiderType<BuildersType::ComunicationBuilder,Test::FILENAME,Test::CLASSNAME,Test::TestClass> aBuilder;
    AnotherBuilder<Test::FILENAME,Test::CLASSNAME> aAnotherBuilder;
return 0;
}

1 个答案:

答案 0 :(得分:2)

您的类模板BuiderType具有类型为BuildersType的非类型模板参数和一组名为Args的类型模板参数,但您的专门化具有两个非类型模板参数{{1} }和FILENAME(你使用它们来实际专门化CLASSNAME)。在声明/定义BuiderType的行中,您使用了一组与aBuilder声明不兼容的模板参数,因为除了第一个之外,您没有任何非类型模板参数。

此代码段具有相同的行为:

template<BuildersType, class ... Args> class BuiderType;