首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c++11 -Template MetaProgramming - Error:未用于部分专门化的模板参数

c++11 -Template MetaProgramming - Error:未用于部分专门化的模板参数
EN

Stack Overflow用户
提问于 2016-02-26 17:20:54
回答 1查看 87关注 0票数 0

我被一个部分模板实现困住了,它的思想是提供一个类(一个构建器),它在编译时使用枚举类定义进行选择,我还想提供文件名和来自工厂单例管理的类名。但这不是编译,我尝试了几个小时,但我看不出我做错了什么。这是代码:

代码语言:javascript
复制
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;
}

汇编输出:

代码语言:javascript
复制
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'

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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-26 17:41:39

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

这个片段具有相同的行为:

代码语言:javascript
复制
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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35657914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档