请考虑以下代码段:
template<unsigned... IDs>
class MyClass{
public:
static const std::array<unsigned, sizeof...(IDs)> ids { IDs... };
PinIDs() = default;
};
然后使用该类:
MyClass<1,5,7,9> myClass;
目标是使ids
的大小为4,并且分别包含值:(1,5,7,9)。
这种类型的对象是否可能,或者我是否必须删除静态限定符?如果不是如何用静态限定符写这个。该对象需要是默认的可构造的。
我尝试了Apple Apple的第一个解决方案,在Win7上使用MS Visual Studio 2017 CE,我得到了这个编译错误:
1>------ Build started: Project: PracticeMath, Configuration: Debug Win32 ------ 1>stdafx.cpp 1>PracticeMath.cpp 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2988: unrecognizable template declaration/definition 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2143: syntax error: missing ';' before '<' 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2059: syntax error: '<' 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2039: 'ids': is not a member of '`global namespace'' 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2143: syntax error: missing ';' before '{' 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2447: '{': missing function header (old-style formal list?) 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(38): error C2065: 'myId': undeclared identifier 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(39): error C2065: 'myId': undeclared identifier 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(40): error C2065: 'myId': undeclared identifier 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(44): error C2065: 'c': undeclared identifier 1>Done building project "PracticeMath.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
完整的原始来源如下:
#include <iostream>
#include <array>
template<unsigned... IDs>
class PinIDs{
public:
static const std::array<unsigned, sizeof...(IDs)> ids;
PinIDs() = default;
const unsigned& operator[]( unsigned idx ) const {
return ids[idx];
}
};
template<unsigned... IDs>
const std::array<unsigned, sizeof...(IDs)> PinIDs<IDs...>::ids { IDs... };
int main() {
PinIDs<4, 17, 19> myId;
std::cout << myId[0] << " ";
std::cout << myId[1] << " ";
std::cout << myId[2] << " ";
std::cout << "\nPress any key and enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
}
感谢StoryTeller提出的事实,当我尝试应用Apple Apple的1st method
时,我意外地混淆了MyClass
而不是我的解决方案中的类的实际名称 - 项目。一旦我纠正它确实编译,构建并按预期运行。
你可以试试这个
#include <array>
template<unsigned... IDs>
class MyClass{
public:
static const std::array<unsigned, sizeof...(IDs)> ids;
MyClass() = default;
};
template<unsigned... IDs>
const std::array<unsigned, sizeof...(IDs)> MyClass<IDs...>::ids {IDs...};
int main(){
MyClass<1,5,7,9> myClass;
return myClass.ids[0];
}
或使用constexpr / inline(都需要c ++ 17)
#include <array>
template<unsigned... IDs>
class MyClass{
public:
//static constexpr std::array<unsigned, sizeof...(IDs)> ids{IDs...};//or this
static inline const std::array<unsigned, sizeof...(IDs)> ids{IDs...};
MyClass() = default;
};
int main(){
MyClass<1,5,7,9> myClass;
return myClass.ids[0];
}
有关基础知识,请参阅@apple apple的答案。我将添加C ++ 17方法来实现它。这与你原来的尝试非常接近。只需在变量中添加一个inline
说明符:
template<unsigned... IDs>
class MyClass{
public:
static inline const std::array<unsigned, sizeof...(IDs)> ids{ { IDs... } };
MyClass() = default;
};
现在声明可以兼作定义。哦,请注意大括号。 std::array
需要初始化为汇总。所以{}
的一对std::array
,以及它所拥有的内部原始阵列的一对。
你为什么不试试online compiler, supporting c++17?
template<unsigned... IDs>
class MyClass{
public:
static constexpr std::array<unsigned, sizeof...(IDs)> ids { IDs... };
MyClass() = default;
};
工作良好。对于在编译时计算的变量,你不需要static const inline
,只需使用static constexpr