简体   繁体   English

是否可以使用模板类的单个参数将类型和该类型的指针传递给c ++模板类?

[英]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.

相关问题 c ++ template使用模板内类的指针的不完整类型 - c++ template Incomplete type using pointer of class inside template 在 C++ 中的模板实例化中使用带有构造函数的类作为类型参数 - Using a class with a constructor as a type parameter in a template instantiation in c++ C ++:朋友模板类/模板非类型参数 - C++: friend template class / template non-type parameter 具有一个更多模板参数的C ++模板类类型声明 - C++ template class type declaration with one more template parameter 在C ++中声明该类的对象时,我可以将模板类'指针作为参数吗? - Can I have the template class' pointer as a parameter when declaring an object of that class' type in C++? 在C ++中,为什么不能使用另一个类的模板类型来管理模板类成员函数? - In C++, why isn't it possible to friend a template class member function using the template type of another class? 如何检查类型是否是C ++中模板类的类型参数? - How to check if a type is a type parameter for template class in C++? C++ class 模板与非类型参数取决于使用 C++11 的类型参数问题 - C++ class template with non-type parameter depending on type parameter problem using C++11 将指针作为通用类型C ++模板传递 - Pass pointer as generic type c++ template c ++扣除“非类型指针函数”类模板参数 - c++ deduction of “non type pointer to function” class template parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM