jag*_*ire 6 c++ templates language-lawyer c++14
我正在阅读C++ 14标准的模板部分,试图提高我对该主题的理解,并偶然发现了这个特定的规则:
§14.1
12模板参数不应由同一范围内的两个不同声明给出默认参数.
[例:
Run Code Online (Sandbox Code Playgroud)template<class T = int> class X; template<class T = int> class X { /?... ?/ }; // error- 结束例子]
对于我的(相对不知情的)阅读,"相同范围"的规范意味着能够在不同的范围内声明模板,而不是定义模板.
C++标识了五种范围:函数,函数原型,本地,命名空间和类.
其中,我的理解是:
因此,允许声明超出定义范围的潜在奇怪情况(可能具有更改的默认参数,取决于范围!)似乎完全落在命名空间范围的肩上.我试验了一下:
[ Coliru ]
命令:
g++ -std=c++1z -O2 -Wall -pedantic -pthread main.cpp && ./a.out
Run Code Online (Sandbox Code Playgroud)
码:
#include <iostream>
namespace math{
template <int I, int J>
struct Plus{
constexpr static int Value_ = I + J;
};
template <int I, int J = 5>
struct Minus; // template declaration.
}
// global-scope template declaration?
//template <int I, int J>
//struct math::Minus; // error: does not name a type (if no declaration @ line 9)
// error: invalid use of math::Minus w/o argument list
// (if declaration @ line 9)
namespace math{
template <int I, int J>
struct Minus{
static int value();
};
namespace{ // anonymous namespace
//template <int I, int J = 5>
//struct Minus; compiles, but is a declaration of another class,
// which I assume hides the math scope class
// error: incomplete type--I assume this means the above
// doesn't declare math::Minus, but math::<anon>::Minus
//constexpr int twominus5= Minus<2>::value();
} // end anonymous namespace
} // end math namespace
//template <int I, int J>
//class math::Minus; // error: invalid use of math::Minus w/o argument list
template <int I, int J>
int math::Minus<I,J>::value(){return I - J;}
int main()
{
std::cout
<< math::Minus<5,1>::value() << std::endl
<< math::Minus<0>::value() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
4
-5
Run Code Online (Sandbox Code Playgroud)
......在阅读标准的这个小片段之前,声明规则似乎符合我的预期.显然,我的理解在某处是错误的.我怀疑是初次阅读c ++标准的模板默认参数声明子句,还是我错过了一些在原生范围之外声明模板的方法?
当然,像这样的语言(如果它确实存在)的奇怪角落应该谨慎使用并且非常谨慎,特别是因为它会改变其他地方依赖于最适用范围的部分指定模板的行为(它会导致名称冲突问题吗? ?如果在模板定义的范围内有默认参数,那么在没有默认声明的作用域中,如果一个完全限定的名称甚至会如何解析呢?),但它引起了我的好奇心.
我会使用别名,因为这对所有参与者来说都不那么模棱两可,但正如我上面所说的那样:我现在很好奇这是一个奇怪的语言特征,我完全没有故意使用,或者是我想象的非特征.
我不确定我能否完全理解您的想法,但我认为该标准只是使用了过于清晰的措辞。这可能是为了澄清不同范围内的“相同”模板可能具有不同的默认参数。例子:
namespace A
{
template< int = 42 > struct X;
}
namespace B
{
template< int = 123 > struct X;
namespace C
{
template< int = 0 > struct X;
}
}
Run Code Online (Sandbox Code Playgroud)
当然,这些模板不是同一个模板(尽管初学者乍一看可能会这么认为),但它们是不同的模板。该标准的措辞很可能只是为了强调这一点。
可以有不同默认值的一个示例是模板别名using:
#include <iostream>
#include <type_traits>
namespace A
{
template< int I = 42 >
struct X { static void f() { std::cout << I << std::endl; } };
}
namespace B
{
template< int I = 0 >
using X = A::X< I >;
}
int main()
{
A::X<>::f();
B::X<>::f();
static_assert( std::is_same< B::X<>, A::X<0> >::value, "Oops" );
}
Run Code Online (Sandbox Code Playgroud)
问题是,它一开始看起来与您的描述相符,但有趣的是,虽然B::X<>和A::X<0>是相同类型,但目前与B::X不是同一模板。请参阅此答案以获取更多信息。A::X
不过,有一个 DR(CWG 问题 1286)可以解决此问题。不同的默认参数 OTOH 是 DR 中提到的一个问题,因此即使 DR 得到解决,它也可能不允许不同的默认值。
| 归档时间: |
|
| 查看次数: |
472 次 |
| 最近记录: |