gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:36:51 25 4
gpt4 key购买 nike

如果一个人有

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com