简体   繁体   English

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

[英]c++11 -Template MetaProgramming - Error: template parameters not used in partial specialization

i am getting stuck with a partial template implementation, the idea is to provide a class (a builder) that is select at compile time using an enum class definition, also i want to provide the name of file and a class name for manage from the factory singleton. 我陷入了部分模板实现的困境,想法是提供一个使用枚举类定义在编译时选择的类(生成器),我也想提供文件名和一个类名,以便从工厂单身人士。 But this is not compiling, i am trying several hours, but i cannot see what i am doing wrong. 但这没有编译,我正在尝试几个小时,但是我看不到我在做什么错。 This is the code: 这是代码:

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

The compile ouput : 编译输出:

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'

At this hours i am really tired, and i am asking help. 在这个时候,我真的很累,我正在寻求帮助。 Thx in advance //===================================================== For simplicity I will post the code with the solution: 提前谢谢// ============================================= ========为了简单起见,我将使用解决方案发布代码:

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

Your class template BuiderType has a non type template parameter of type BuildersType and a pack of type template parameters named Args but your specialization has two non type template parameters FILENAME and CLASSNAME (where you use non of them to actually specialize BuiderType ). 您的类模板BuiderType具有一个类型为BuildersType的非类型模板参数,以及一包名为Args的类型模板参数,但是您的专业化对象有两个非类型模板参数FILENAMECLASSNAME (在其中,您可以使用其中的一个非类型来实际地专门化BuiderType )。 In the line where you declare/define aBuilder you use a set of template parameters that is incompatible to the declaration of template<BuildersType, class ... Args> class BuiderType; 在声明/定义aBuilder的行中,使用了一组与template<BuildersType, class ... Args> class BuiderType;的声明不兼容的模板参数template<BuildersType, class ... Args> class BuiderType; because here you do not have any non type template parameters besides the first one. 因为在这里除了第一个参数外,您没有任何非类型模板参数。

This snippet has the same behaviour: 此代码段具有相同的行为:

template<class ... Args> class A;

// this specialization has a template parameter that 
// 1. cannot and
// 2. will not be used in the parameter list for A
template<int I> class A<int> { };

int main()
{

  A<int> a; // error: A is an incomplete type
  A<2> b;   // error: '2' is not a type but 
            // A template expects type parameters

  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM