c++11 - 模板元编程 - 错误 : template parameters not used in partial specialization

标签 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'

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

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

最佳答案

您的类(class)模板 BuiderType有一个类型为 BuildersType 的非类型模板参数和一组名为 Args 的类型模板参数但是你的特化有两个非类型模板参数 FILENAMECLASSNAME (你不使用它们来实际专门化 BuiderType )。在您声明/定义 aBuilder 的行中您使用了一组与 template<BuildersType, class ... Args> class BuiderType; 的声明不兼容的模板参数因为这里除了第一个之外没有任何非类型模板参数。

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

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

关于c++11 - 模板元编程 - 错误 : template parameters not used in partial specialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35657914/

相关文章:

c++ - 有没有一种安全的方法可以在 std::future 上调用 wait()?

c++ - 将对象转换为其基数的子集

c++ - C++ 中的 Constexpr 迭代器

c++ - 是否可以检查类型是否已使用特定模板参数实例化?

c++ - 在几个 QMainWindows 之间共享 QUndoStack?

c++ - 我如何学习 Visual C++?

c++ - 从具有可变数量的模板参数的模板化类中的特定基础获取结果

c++ - std::unique_ptr,自定义删除器和类型更改

c++ - 迭代多个嵌套 vector

c++ - constexpr 与静态 const : Which one to prefer?