模板别名上的is_same_template行为奇怪
Strange behaviour of is_same_template on template aliases
以下程序...
#include <iostream>
#include <type_traits>
template <typename T>
struct Template{};
template <typename T>
using Alias = Template<T>;
template
<
template <typename>
class T1,
template <typename>
class T2
>
struct is_same_template : std::false_type{};
template
<
template <typename>
class T
>
struct is_same_template<T, T> : std::true_type{};
int main() {
std::cout << std::boolalpha;
std::cout << "Template == Template: " << is_same_template<Template, Template>::value << std::endl;
std::cout << "Template == Alias: " << is_same_template<Template, Alias>::value << std::endl;
std::cout << "Alias == Alias: " << is_same_template<Alias, Alias>::value << std::endl;
}
。输出。。。
Template == Template: true
Template == Alias: false
Alias == Alias: true
。使用 G++ 4.8.1、Clang 3.4 和 VC++ 18.00.21005.1 编译。
是这些编译器中的错误还是标准的要求?
这是标准要求的行为,这是完全合乎逻辑的。别名模板不是模板别名(尽管有些人打算这样做)。最初,即使在标准中似乎也存在一些混淆,请参阅 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1244 。
在当前的标准化表单中,别名模板类似于其非模板化的对应部分:它为类型设置别名。在模板版本中,类型可能相关。
并立即被取代。例如,Alias<T>
T
本身就是模板参数,它将是依赖类型Template<T>
- 从这个意义上说,名称别名模板可能有点混乱,因为它表明会在某个时候实例化别名声明。但实际上别名模式会立即被替换 - 从这个意义上说,模板化版本更像是一个始终存在且不需要实例化的依赖别名声明,而不是别名声明模板。
不过,在这一点上,我们用这些术语的确切含义变得有点哲学化。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 传递给std::function template的template参数究竟代表什么
- 在template中使用std::variant的template函数
- 为什么 Same<T, U> 不包含 Same<U, T>?
- Trie*& 和 Trie** 的意思是 Same?
- C++ template for QList
- C++ - 为什么这里需要'template'关键字?
- 使用"std::enable_if_t" "function template has already been defined"
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- C++模板错误:"invalid explicitly-specified argument for template parameter"
- C++ class template
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- 为什么编译器说"candidate template ignored: couldn't infer template argument 'InputIterator'"?
- 为什么在这种情况下我需要 .template
- std::span constructor, libcxx vs libstdc++, template vs non-
- 模板函数参数到模板函数的"candidate template ignored: could not match ..."
- MacOS 上的 LLVM - 标准文件 iosfwd 中未知类型名称'template'
- std::get like (partial) template specialization
- C++ "Same Letter"代码无法正常工作
- 实现多类型算术运算符时如何解决"template argument deduction/substitution failure"