[英]Is it possible to pass a type and a pointer of that type to a c++ template class using a single parameter of a template class?
Is it possible to pass a type and a pointer of that type to a c++ template class using a single parameter of a template class? 是否可以使用模板类的单个参数将类型和该类型的指针传递给c ++模板类?
I want to take a pointer to a embedded hardware address (an uart) which has the type UART_HandleTypeDef
and deduce that type information instead of manually declaring it. 我想使用一个指向嵌入式硬件地址(一个uart)的指针,该地址的类型为
UART_HandleTypeDef
并推断出该类型信息,而不是手动声明它。 Something akin to: 类似于:
template<typename T> class serial{
public:
T::value_type* uart = T;
};
I want to get away from the normal notation which would require me to state the type and then pass a pointer: 我想摆脱通常的表示法,这需要我声明类型然后传递一个指针:
template<typename T,T* ptr> class c{
public:
T* _ptr = ptr;
};
update: I forgot to mention: pre C++11 is supported by my compiler. 更新:我忘了提一下:我的编译器支持C ++ 11之前的版本。 It supports some C++11 features
它支持某些 C ++ 11功能
Since C++17, you might have 从C ++ 17开始,您可能已经
template <auto* ptr> class c
{
public:
auto _ptr = ptr;
};
Before that, 在那之前,
template <typename T, T* ptr> class c
{
public:
T* _ptr = ptr;
};
is the way to go. 是要走的路。
MACRO can help since C++11 自C ++ 11起,MACRO可以提供帮助
#define TEMPLATE_AUTO(...) decltype(__VA_ARGS__), __VA_ARGS__
c<TEMPLATE_AUTO(my_ptr)> v;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.