当前位置: 首页 > 知识库问答 >
问题:

为什么我的函数模板专业化被VS2017拒绝,而不是被VS2015拒绝?

程天佑
2023-03-14

我有一个将类型与整数值相关联的trait类。

struct traits
{
  private:
    template<int ID> struct type_impl {};
    template<> struct type_impl<1> { using type = int; };
    // ...

  public:
    template<int ID> using type = typename type_impl<ID>::type;

};

我正在编写一个模板函数,返回类型由上面的traits类提供,并将其专门化为各种int值:

  template<int ID> traits::type<ID> function();
  template<> inline traits::type<1> function<1>() { return 42; };
  // ...

这可以与VS2015一起编译(参见https://godbolt.org/z/LpZnni)但VS2017并没有抱怨:

错误C2912:显式专门化的int函数

令我惊讶的是,像下面这样声明一个非模板函数会编译:

traits::type<1> other_function();

traits::type_impl公开可以解决编译问题,但我不明白为什么。对我来说,other_function的专门化和声明都应该使用traits::type_implprivate或none编译。

谢谢你的帮助。

@rubenvb评论之后的进一步调查我知道我发布的代码是非法的,所以我尝试进行部分专门化(我认为这是合法的):

struct traits
{
  private:
    template<int ID,bool=true> struct type_impl {};
    template<bool B> struct type_impl<1,B> { using type = int; };
    // ...

  public:
    template<int ID> using type = typename type_impl<ID>::type;

};

template<int ID> traits::type<ID> function();
template<> inline traits::type<1> function<1>() { return 42; };

现在每个编译器都很高兴,但是VS2017仍然希望特性::type_impl公开。我想这是一个Visual Studio错误。

共有1个答案

蓬弘
2023-03-14

你有这个密码吗

struct traits
{
  private:
    template<int ID> struct type_impl {};
    template<> struct type_impl<1> { using type = int; }; // HERE

  public:
    template<int ID> using type = typename type_impl<ID>::type;
};

template<int ID> traits::type<ID> function();
template<> inline traits::type<1> function<1>() { return 42; };

标有//INE的行包含类内模板特化。这在C中是非法的。

我们从中了解到,当涉及模板时,VisualStudio会发出可怕的错误消息。如果问题还不清楚,请看另一个编译器的说法。不同的编译器通常会指向不同的问题或以不同的方式讨论它们,这至少可以很好地提示实际问题的来源。

英特尔编译器显示:

error: explicit specialization is not allowed in the current scope
  template<> struct type_impl<1> { using type = int; };
  ^

GCC显示了这一点:

error: explicit specialization in non-namespace scope 'struct traits'
    5 |     template<> struct type_impl<1> { using type = int; };
      |              ^

出于某种原因,叮当声似乎并不介意。这似乎是一个错误。

 类似资料:
  • 我有以下示例代码,简化为基本代码,它使用GCC6.1、GCC7.0head和Visual Studio 2015/2017 RC编译,但不使用任何clang版本。 clang告诉我:prog.cc:12:34:error:'_p'是'outer::foo'返回std::make_tuple(c_p...)的私有成员;

  • 我有两个服务,Spring启动docker,当我尝试与rest模板通信时,我得到了java.net.ConnectException:连接拒绝(连接拒绝) url为http://localhost:8081/api/v1/package/250Mbps 服务1 docker compose。yml: 服务2 docker compose。yml: Rest模板URL,它是工作时,我运行项目2从In

  • 我有一个问题。我有操作系统CentOS 5.8。我已经在httpd.conf文件中编写了这个配置: 听 85 在文件末尾: selinux是许可的。 以下是apache使用的目录和文件: 当我打电话的时候 '服务器ip':85/wsgi 从浏览器,有我的服务器的ip而不是'服务器ip ',我得到消息:(403)禁止:你没有权限访问/wsgi在这个服务器上。 和/var/log/httpd/erro

  • 在让IAM策略为我的SQS队列工作时遇到问题,我尝试了IAM策略模拟器。无论我的政策多么简单,它总是说它被拒绝了。 以下政策旨在允许对任何SQS资源采取任何行动。这是我附加到特定用户的一个策略,这就是没有指定主体的原因。 当我尝试在策略模拟器中运行这个列表队列操作时,我会被拒绝:隐式拒绝(没有匹配的声明)。这种说法怎么可能不符合呢? 以下是策略编辑器上被拒绝的截图:

  • lambda执行角色具有对51个函数的s3访问权限,包括ListBuckets和所有其他读操作。 我的S3 bucket有一个允许从lambda角色访问的策略。(反正是在同一个帐户中,所以我不认为这是必需的)。 我甚至只是为了好玩才让桶公共访问。 这是λ码。我不知道为什么bucket.objects.all()无法访问S3。 Lambda执行角色策略

  • 问题内容: 这是我的编辑从第27行到第39行的代码: 我认为我的问题可能与Win7教授有关:(访问被拒绝) 如何解决这个问题,或者我需要做些什么或阅读才能使它起作用? 谢谢你不燃烧。 我只是更改了文件夹选项,使我获得完整的(Access …),现在我只需要弄清楚为什么在运行javac VendingMachine.java时为什么没有得到任何输出,我想是有一个新问题。 问题答案: 您的工作目录为。