C ++模板别名和部分模板类专长

时间:2018-08-24 08:08:09

标签: c++ g++ language-lawyer clang++

当使用模板别名作为声明部分专业化的模板类的参数时,我面临着某种意外的结果。在某些情况下,我什至发现GCC 8.2和Clang 6.0.0在结果上并不一致。可以在下面找到说明我的案例的代码。

// A template struct named Foo.
template<typename>
struct Foo {};

// A template alias for type Foo.
template<typename TYPE>
using AliasFoo = Foo<TYPE>;

// A variadic template alias for type Foo.
template<typename... ARGS>
using VariadicAliasFoo = Foo<ARGS...>;

// A template struct named Bar.
template<template<typename...> class TEMPLATE, typename TYPE>
struct Bar
{
  static constexpr bool value = false;
};

// Partial specialization of template struct Bar.
template<template<typename...> class TEMPLATE, typename... ARGS>
struct Bar< TEMPLATE, TEMPLATE<ARGS...> >
{
  static constexpr bool value = true;
};


// The main function.
int main()
{
  // These compile time assertions are satisfied (as expected).
  static_assert(Bar<Foo, Foo<int> >::value, "");
  static_assert(Bar<Foo, AliasFoo<int> >::value, "");
  static_assert(Bar<Foo, VariadicAliasFoo<int> >::value, "");

  // These compile time assertions fails with Clang 6.0.0 but succeed with GCC 8.2
  static_assert(Bar<AliasFoo, Foo<int> >::value, "");
  static_assert(Bar<AliasFoo, AliasFoo<int> >::value, "");
  static_assert(Bar<AliasFoo, VariadicAliasFoo<int> >::value, "");

  // These compile time assertions fails with Clang 6.0.0 and GCC 8.2.
  static_assert(Bar<VariadicAliasFoo, Foo<int> >::value, "");
  static_assert(Bar<VariadicAliasFoo, AliasFoo<int> >::value, "");
  static_assert(Bar<VariadicAliasFoo, VariadicAliasFoo<int> >::value, "");

  // Always return zero.
  return 0;
}

接下来的问题是,根据标准(c ++ 17),以上代码中最后六个编译时断言的结果应该是什么?

0 个答案:

没有答案