C++模板参数类型推断

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

I have such a template in C++

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

所以我可以这样使用它:

so I can use it as such:

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

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

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

But I don't want to specify the type int or identity i twice, I want to use just

Ptr<&i> ptr;

并让编译器自己找出 int 类型部分.

and let the compiler figure out the int type part by itself.

我如何声明我的模板来做到这一点?

How can I declare my template to do that ?

我读过这个问题,但答案是使用宏,这不好:模板c++的模板?

I've read this question but the answer is using macros, that's not nice: template of template c++?

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

can I do this by just template without macros ? I'm using Visual C++ 2013.

推荐答案

UPDATE

c++17 引入了P0127R2 使用 auto 声明非类型模板参数",允许声明非类型模板使用 auto 作为实际类型占位符的参数:

c++17 introduced "P0127R2 Declaring non-type template parameters with auto", allowing to declare a non-type template parameter(s) with auto as a placeholder for the actual type:

template <auto P> struct Ptr {};

也就是说,P 是一个非类型模板参数.它的类型可以通过 decltype(P) 推断出来.

That is, P is a non-type template parameter. Its type can be inferred with decltype(P).

auto 遵循众所周知的推导和偏序规则.在您的情况下,可以将类型限制为仅接受指针:

auto in a template parameter list is subject to well-known deduction and partial ordering rules. In your case, the type can be constrained to accept pointers only:

template <auto* P> struct Ptr {};

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

Note that the syntax utilizing auto is sufficient even for more detailed inspection, e.g.:

template <typename F>
struct FunctionBase;

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

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

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

It's also possible to use the inferred type as a contraint for other template parameters:

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

<小时>

旧答案

既然您问的是一个没有宏定义帮助的基于类模板的纯解决方案,那么答案很简单:至于现在(2014 年 12 月,c++14) 不可能.

Since you are asking about a pure class template-based solution without the help of macro definitions then the answer is simple: as for now (Dec 2014, c++14) it is not possible.

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

This issue has been already identified by the WG21 C++ Standard Committee as a need and there are several proposals to let templates automatically infer the type of non-type template arguments.

最接近的是N3601 隐式模板参数:

此示例的目的是消除对冗余 template 习语的需要.这个习语被广泛使用,在谷歌上的点击量超过 10 万次.

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.

目标是能够替换像template这样的模板声明.struct C; 和另一个声明,这样我们就可以像 C<&X::f> 一样实例化模板,而不必说 C.

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>.

基本思想是能够说template;struct C {/* ... */}; 表示应该推导出T.为了更详细地描述,我们考虑一些模板类和函数的扩展示例.

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.

[...]

关键思想是传递第二个模板参数的类型是冗余信息,因为它可以使用第二个类型参数的普通类型推导来推断.考虑到这一点,我们建议在模板参数前面加上 using 表示它不应该作为模板参数显式传递,而是从后续的非类型模板参数中推导出来.这立即使我们能够如下改进 describe_field 的可用性.

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 模板花絮:

激励示例是一个假定的反射类型特征,它提供了类成员的属性.

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

问题是describe 的声明应该是什么样子的?" 由于它需要一个非类型模板参数,我们需要使用熟悉的(100k hits onGoogle) template" 习惯用法

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;

[...]

我们的关键思想是传递第二个模板参数的类型是(几乎总是)冗余信息,因为它可以使用从第二个类型参数的普通类型推导来推断.考虑到这一点,我们建议允许 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.

The current status of both proposals can be tracked under EWG issue 9.

还有其他一些讨论 建议使用 auto 的替代语法:

There are some other discussions proposing alternative syntax with auto:

template <auto T> struct describe;

相关文章