I have a the following class template
template<int N>
constexpr int arraySize() { return arraySize<N-1>() + N; }
template<>
constexpr int arraySize<0>() { return 0; }
template<int C>
class MyClass {
public:
std::array<int, arraySize<C>()> arr;
};
int main() {
MyClass<3> cls;
std::cout << cls.arr.size() << std::endl; // Output: 6
}
Everything works but I would like to have calculateArraySize<N>()
as a member function. I've tried the following:
template<int C>
class MyClass {
public:
static constexpr int arraySize();
std::array<int, MyClass<C>::arraySize()> arr;
};
template<int C>
constexpr int MyClass<C>::arraySize(){ return MyClass<C-1>::arraySize() + C; }
template<>
constexpr int MyClass<0>::arraySize() { return 0; }
Results in the following error:
fatal error: recursive template instantiation exceeded maximum depth of 1024 std::array::arraySize()> arr;
template<int C>
class MyClass {
public:
template<int N>
static constexpr int arraySize();
std::array<int, MyClass::arraySize<C>()> arr;
};
template<int C>
template<int N>
constexpr int MyClass<C>::arraySize(){ return MyClass::arraySize<N-1>() + N-1; }
template<int C>
template<>
constexpr int MyClass<C>::arraySize<0>() { return 0; }
Gives the following error:
tmp.cc:19:27: error: cannot specialize (with 'template<>') a member of an unspecialized template constexpr int MyClass::arraySize<0>() { return 0; }
Is it possible to achieve the desired behaviour? Solutions using C++14/C++17 features (I guess it should be possible usinn if-constexpr) are welcomed but won't solve my particular problem since only C++11 is available.
Aucun commentaire:
Enregistrer un commentaire