gpt4 book ai didi

C++模板参数类型推断

转载 作者:可可西里 更新时间:2023-11-01 15:54:53 28 4
gpt4 key购买 nike

我在C++中有这样一个模板

template<typename T, T* P> struct Ptr {};

所以我可以这样使用它:

const int i = 0;
Ptr<int, &i> ptr;

Ptr<decltype(i), &i> ptr;

但我不想指定类型 int 或标识 i 两次,我只想使用

Ptr<&i> ptr;

让编译器自己找出int类型的部分。

我怎样才能声明我的模板来做到这一点?

我读过这个问题,但答案是使用宏,这不太好: template of template c++?

我可以只通过没有宏的模板来做到这一点吗?我正在使用 Visual C++ 2013。

最佳答案

更新

引入了“P0127R2 Declaring non-type template parameters with auto”,允许使用 auto 声明非类型模板参数作为实际类型的占位符:

template <auto P> struct Ptr {};

P是一个非类型模板参数。它的类型可以用 decltype(P) 推断出来。 .

auto模板参数列表中的元素遵循众所周知的推导和偏序规则。在您的情况下,可以将类型限制为仅接受指针:

template <auto* P> struct Ptr {};

请注意,使用 auto 的语法甚至对于更详细的检查也足够了,例如:

template <typename F>
struct FunctionBase;

template <typename R, typename... Args>
struct FunctionBase<R(*)(Args...)> {};

template <auto F>
struct Function : FunctionBase<decltype(F)> {};

也可以使用推断类型作为其他模板参数的约束:

template <auto I, decltype(I)... Is>
struct List {};

旧答案

既然你问的是一个没有宏定义帮助的纯类模板解决方案,那么答案很简单:就目前而言(2014 年 12 月,)不可能

这个问题已经被 WG21 C++ 标准委员会确定为一个需要,并且有几个提议让模板自动推断非类型模板参数的类型。

最接近的是N3601 Implicit template parameters :

Implicit template parameters

The purpose of this example is to eliminate the need for the redundant template<typename T, T t> idiom. This idiom is widely used, with over 100k hits on Google.

The goal is to be able to replace a template declaration like template<typename T, T t> struct C; with another declaration so that we can instantatiate the template like C<&X::f> instead of having to say C<decltype(&X::f), &X::f>.

The basic idea is to be able to say template<using typename T, T t> struct C {/* ... */}; to indicate that T should be deduced. To describe in more detail, we consider some extended examples of template classes and functions.

[...]

The key idea is that passing the type of the second template parameter is redundant information because it can be inferred using ordinary type deduction from the second type parameter. With that in mind, we propose that prefacing a template parameter with using indicates that it should not be passed explicitly as a template argument but instead will be deduced from subsequent non-type template arguments. This immediately allows us to improve the usability of describe_field as follows.

template<using typename T, T t> struct describe_field { /* ... */ };
/* ... */
cout << describe_field<&A::f>::name; // OK. T is void(A::*)(int)
cout << describe_field<&A::g>::arity; // OK. T is double(A::*)(size_t)

一项类似的提议包含在 N3405 Template Tidbits 中。 :

T for two

The motivating example is a putative reflection type trait giving properties of a class member.

struct A {
void f(int i);
double g(size_t s);
};
/* ... */
cout << describe<&A::f>::name; // Prints "f"
cout << describe<&A::g>::arity; // prints 1

The question is "what should the declaration of describe look like?" Since it takes a non-type template parameter, we need to specify the type of the parameter using the familiar (100k hits on Google) “template<class T, T t>” idiom

template<typename T, T t> struct describe;

[...]

Our key idea is that passing the type of the second template parameter is (nearly always) redundant information because it can be inferred using ordinary type deduction from the second type parameter. With that in mind, we propose allowing describe to be declared as follows.

template<typename T t> struct describe;
/* ... */
cout << describe<&A::f>::name; // OK. T is void(A::*)(int)
cout << describe<&A::g>::arity; // OK. T is double(A::*)(size_t)

可以在 EWG issue 9 下跟踪这两个提案的当前状态。 .

还有一些其他的discussions使用 auto 提出替代语法:

template <auto T> struct describe;

关于C++模板参数类型推断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27372492/

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