模板参数可以是int还是unsigned long?

时间:2017-03-02 15:07:51

标签: c++ templates

我有以下代码:

#include <iostream>

template<size_t N>
class A
{
};

template<int N, typename T> class B;

template<int N>
class B<N, A<N>>
{
};

int main()
{
    B<3, A<3>> b;
    return 0;
}

此处B模板int,而A模板size_t模板unsigned long,这是我正在使用的两个编译器的B

当我使用编译器1(当前编译器)时,一切都按照我期望的方式编译和工作。当使用编译器2(我们要移动到的那个)时,我收到编译器错误,指出unsigned long没有采用3的模板特化 - 它已将unsigned long解释为一个A,因为它需要B,但是找不到B的任何内容。修复是显而易见的 - 只需更改size_t以获取A(或更改int以获取<?php $result = ''; foreach($project->tags()->split(',') as $tag) { $result .= sprintf('%s,', $tag); } //Will remove the last character of the string. $result = substr($result, 0, -1); ?> <a href="#" data-tag="<?php echo $result; ?>">Project</a>'; ) - 但我想知道哪个是严格正确的标准。我的直觉是它是编译器2(抛出错误的那个)。

1 个答案:

答案 0 :(得分:4)

来自[temp.deduct.type]:

  

如果P的表单包含<i>,并且i的类型与封闭的简单指定的模板的相应模板参数的类型不同-template-id ,扣除失败。

A<N>的{​​{1}} Nint应该失败的,因为A的相应模板参数实际上是size_t。这是编译器#1的错误。