c++ - 推导引用模板参数的类型

标签 c++ c++11 c++14 c++17

如果一个人有

template <class T>
class A{};

// global namespace, static storage duration
static constexpr A<int> a;

是否可以推导出类型 A<int>路过a作为引用模板参数,例如:

// This question asks to make the syntax in the following line compile:
static_assert(std::is_same<A<int>, typename GetReferenceType<a>::type>::value, "");
// I am aware the next line works, but it's not what I am looking for in this question
static_assert(std::is_same<A<int>, decltype(a)>::value, "");

// This is pseudo code of how this could work in theory
template <const T& RefT, class T> // I know this does not compile, but this shows what I want
struct GetReferenceType{          // which is automatically deduce type `T` without having to 
  using type = T;                 // write it out
};

解释为什么这在 C++ 中不可能的答案与编译此语法的解决方案一样受欢迎:) 我主要是出于好奇而问,因为基本上其他所有内容都可以在模板中推断出来,但显然不是引用类型。

这也应该有效,但不满足上述语法要求:

template <class T>
constexpr auto GetReferenceTypeFunc(const T& t) -> T;

static_assert(std::is_same<A<int>, decltype(GetReferenceTypeFunc(a))>::value, "");

为什么我要这样做

我力求最简洁的语法。 而Instantiate<decltype(a)>有效,但它在简洁性方面排名并不高,尤其是像 Instantiate<a> 这样的语法。是可能的。

想象一下a没有短类型A<int>但类似的东西 A<OmgWhyIsThisTypeNameSoLong> .

然后,如果你想实例化一个类型 A<OmgWhyIsThisTypeNameSoLong> ,你必须写:

Instantiate<A<OmgWhyIsThisTypeNameSoLong>>;

恰好我们已经有了一个全局对象 a ,所以最好不必写那么长的类型,而是 Instantiate<a> .

当然可以选择创建别名 using AOmg = A<OmgWhyIsThisTypeNameSoLong>但我真的很想避免使用另一个与 A 非常相似的名称来垃圾邮件命名空间。 .

最佳答案

在 C++20 中,您可能会这样做:

template <auto V>
struct GetReferenceType
{
    using type = std::decay_t<decltype(V)>;  
};

static_assert(std::is_same<A<int>, GetReferenceType<a>::type>::value);

但是decltype似乎足够了。

Demo

// I not only want to deduce A<int> but also int

所以你可能想要这样的特征:

template <typename> struct t_parameter;

template <template <typename > class C, typename T> struct t_parameter<C<T>>
{
    using type = T;    
};

但简单的替代方法是直接在A中添加信息:

template <class T>
class A{
    using value_type = T;
};

关于c++ - 推导引用模板参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60881423/

相关文章:

c++ - 引用动态分配的变量与指向同一动态分配变量的指针?

c++ - X11 窗口在收到事件之前不会刷新

c++ - 区别: std::make_unique<char>(size_t size) 和 std::make_unique<char[]>(size_t size)?

c++ - 在某些枚举模板情况下启用类构造函数

c++ - 从字符串中删除一个字符

c++ - MFC 上下文菜单的项目高度不正确

c++ - 在 Matrix 类中使用 [][] 进行常量双索引

c++ - 这是制作迭代器的可接受方式吗?

c++ - 从智能指针获取原始指针

c++ - 模板值参数上的 decltype 是否应该触发 SFINAE 上下文?