相关疑难解决方法(0)

在编译时获取std :: array中的元素数

以下是有效的C++代码,为什么不呢?

std::array<std::string, 42> a1;
std::array<int, a1.size()> a2;
Run Code Online (Sandbox Code Playgroud)

它不能在GCC 4.8中编译(在C++ 11模式下).有一个简单但不优雅的解决方法:

std::array<std::string, 42> a1;
std::array<int, sizeof(a1)/sizeof(a1[0])> a2;
Run Code Online (Sandbox Code Playgroud)

很明显,编译器可以计算出std :: array中的元素数量.为什么std :: array :: size()不是constexpr static函数?

编辑:我找到了另一种解决方法:

std::array<std::string, 42> a1;
std::array<int, std::tuple_size<decltype(a1)>::value> a2;
Run Code Online (Sandbox Code Playgroud)

c++

28
推荐指数
2
解决办法
8123
查看次数

constexpr和bizzare错误

我有:

constexpr bool is_concurrency_selected()const
    {
        return ConcurrentGBx->isChecked();//GBx is a groupbox with checkbox
    }
Run Code Online (Sandbox Code Playgroud)

我收到错误:

C:\...\Options_Dialog.hpp:129: error: enclosing class of 'bool Options_Dialog::is_concurrency_selected() const' is not a literal type
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

c++ constexpr c++11

3
推荐指数
1
解决办法
2782
查看次数

标签 统计

c++ ×2

c++11 ×1

constexpr ×1