小编Wal*_*Cat的帖子

c ++模板参数类型推断

我在C++中有这样的模板

template<typename T, T* P> struct Ptr {};
Run Code Online (Sandbox Code Playgroud)

所以我可以这样使用它:

const int i = 0;
Ptr<int, &i> ptr;
Run Code Online (Sandbox Code Playgroud)

要么

Ptr<decltype(i), &i> ptr;
Run Code Online (Sandbox Code Playgroud)

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

Ptr<&i> ptr;
Run Code Online (Sandbox Code Playgroud)

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

如何声明我的模板呢?

我已经阅读了这个问题,但答案是使用宏,这不是很好: 模板c ++的模板?

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

c++ templates type-inference inferred-type

10
推荐指数
1
解决办法
2127
查看次数

标签 统计

c++ ×1

inferred-type ×1

templates ×1

type-inference ×1