模板别名、模板专门化和模板参数

Template alias, Template specialization and Template Template parameters

本文关键字:参数 别名 专门化      更新时间:2023-10-16

我想通过组合使用模板别名和模板专门化来确定模板参数的底层模板。下面的代码在gcc 4.8, 6.2.1上可以正常编译,但在clang 3.5, 3.8上不能正常编译。

#include <iostream>
template <typename T> struct First {};
template <typename T> struct Second {};
template <template <typename> class F, typename T> struct Foo {};
template <typename T> struct Foo<First, T>
{
  void f() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};
template <typename T> struct Foo<Second, T> 
{
  void f() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};
template <typename F, typename T> struct Resolution {};
template <typename T> struct Resolution<First<T>, T>
{
   template <typename P> using type = First<P>;
};
template <typename T> struct Resolution<Second<T>, T>
{
    template <typename P> using type = Second<P>;
};
int main()
{
    Foo<Resolution<First<int>, int>::type, float> my_foo;
    my_foo.f(); // main.cpp:34:12: error: no member named 'f' in 'Foo<Resolution<First<int>, int>::type, float>'
    return 0;
}

哪个行为符合标准?

回答:这是c++标准核心语言中已知的错误,正如tc在评论中所描述的那样。http://wg21.link/cwg1286