数组元素作为非类型模板参数

Array element as non type template argument

本文关键字:参数 类型 数组元素      更新时间:2023-10-16

有可能克服这个问题吗:

                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*的类都必须改变专业化,但你可能不会让它变得更好(并且工作;)