数组元素作为非类型模板参数
Array element as non type template argument
有可能克服这个问题吗:
class Constants
{
static Std_ReturnType dtcSitzheizungSchalteMittelkonsoleHlTasterHaengt( UInt8 const value )
{
return Rte_Call_demSitzheizungSchalteMittelkonsoleHlTasterHaengt_SetEventStatus( value );
}
static DTCSetter * const DTCSETTER_WITH_NO_DID[SEAT_HEATING_DTCS_COUNT]; //how to use an element of this array as a non type template parameter ?
};
template class ButtonStuckPolicy< &Constants::dtcSitzheizungSchalteMittelkonsoleHlTasterHaengt >; //works
在C++03中?
通常,是否可以将静态数组元素作为非类型模板参数传递?
感谢
在C++03中,任何具有外部链接的对象的地址都是公平的。但是值不是,除非它是const
并用整数常量表达式初始化。正如您所拥有的,子对象在C++11之前不需要应用。
您所拥有的是将地址参数传递给int
参数,这在任何上下文中都不起作用。
这在C++11:中是可以的
const int x[100] = {}; // zero-initialize
template<int T>
class foo;
foo<x[0]> y;
这就是:
int x[100];
template<int *T>
class foo;
foo<&x[0]> y;
这在C++03中也可以,因为它使用了整个命名对象的地址:
int x[100];
template<int *T>
class foo;
foo<x> y;
我的解决方案-创建模板:
template <int* arr, int index>
class indexer{
public:
static inline int* element(){ return arr+index; }
};
然后你就说
template <int* arr, int index>
foo{
// Here use indexer<arr,index>::element();
};
int x[10];
foo<x,0> f;
甚至:
template <typename T>
bar{
//here use T::element();
};
int x[10];
bar<indexer<x, 0> > b;
它没有foo<&x[0]>
那么漂亮,而且你所有专门研究int*
的类都必须改变专业化,但你可能不会让它变得更好(并且工作;)
相关文章:
- 扩展C++生成的代码的模板参数类型名称
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 在 C++ 中运行时调用模板时,是否可以切换模板的参数类型?
- 将函数参数类型声明为 auto
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- 在 C++17 中调用具有不同参数类型的构造函数
- 具有先前参数类型匹配的参数包
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- 如何从第一个参数推断第二个参数类型?
- C++模板函数中,指定回调函子/lambda 的参数类型,同时仍允许内联?
- 如何用不同的参数类型和数字回调函数
- C++stoi:这两个重载都无法转换所有参数类型
- 为什么std::{container}::template不能推导其参数类型
- 为模板参数类型中的新对象分配内存
- 为指向成员模板参数的指针推导额外模板参数类型的紧凑方式
- 使用std::conditional中的模板来确定函数参数类型
- C++中的短参数类型