连接一元参数的模板参数包 [英] Concatenating template parameter packs for a unary argument

查看:39
本文介绍了连接一元参数的模板参数包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然说 std :: add_pointer 是一元的,但以下代码已被GCC 7.0.0(20160608)和Clang 3.9.0接受:

Although, say, std::add_pointer is unary, the following code is accepted by both GCC 7.0.0 (20160608) and Clang 3.9.0:

template <typename ...Ts>
struct tc1 {
  using a = std::add_pointer<Ts...>;
};

但是,尽管Clang接受了以下代码,但GCC拒绝了它:

However, while the following code is accepted by Clang, it is rejected by GCC:

template <typename ...Ts>
struct tc2 {
  template <typename ...Us>
  using b = std::add_pointer<Ts...,Us...>;
};

这是有效的C ++吗?从句法上讲,我可以想象当包装为空时,逗号是一个问题,但是大概在其他情况下它会被忽略.例如, std :: common_type 接受零个或多个参数,而对于任何一个编译器,以下内容都没有问题:

Is this valid C++? Syntactically, I could imagine that the comma is a problem when packs are empty, but presumably it is elided on other occasions; for example, std::common_type accepts zero or more arguments, and the following presents no problem for either compiler:

template <typename ...Ts>
struct tc3 {
  template <typename ...Us>
  using c = std::common_type<Ts...,Us...>;
};

推荐答案

您可以将此代码用于任意数量的模板参数 tc3< 1或更多> :: a<零或更多> ,在GCC和Clang上:

You can use this code for any number of template arguments tc3<1 or more>::a<zero or more>, on GCC and Clang:

#include <type_traits>

struct A {
    template<typename ...Args> A(Args ... args) {}
};

template <typename T, typename ...Ts>
struct tc3 {
  template <typename ...Us>
  using c = std::add_pointer<T(Ts...,Us...)>;
};

int main() {
    typedef tc3<A, int, float>::template c<unsigned, double>::type  ptr;// A(*)(int,float,unsigned,double)
    typedef tc3<A>::template c<>::type ptr2;                // A(*)()
    typedef tc3<bool>::template c<int, int>::type ptr3;     // bool(*)(int,int)
    typedef std::add_pointer<bool(int, int)>::type ptr4;    // bool(*)(int,int)

    return 0;
}

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆