将可变参数模板列表中的整数值分配给静态常量 std::array 成员

Assigning integer values from variadic template list to static const std::array member

考虑以下代码片段:

template<unsigned... IDs>
class MyClass{
public:
    static const std::array<unsigned, sizeof...(IDs)> ids { IDs... };
    PinIDs() = default;
};

然后使用class作为:

MyClass<1,5,7,9> myClass;

Objective 将具有大小为 4 的 ids,并分别包含值:(1,5,7,9)。

这种类型的对象是否可能,或者我是否必须删除静态限定符?如果不是,将如何使用静态限定符来编写它。该对象需要是默认可构造的。


编辑:

我尝试了 Apple Apple 的第一个解决方案和 MS Visual Studio 2017 CE on Win7 我收到此编译器错误:

 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 而不是我解决方案中 class 的实际名称- 项目。一旦我更正它确实按预期编译、构建和 运行。

你可以试试这个


#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