【问题标题】:static std::array with structs containing std::string [duplicate]具有包含 std::string 的结构的静态 std::array [重复]
【发布时间】:2017-10-21 03:53:06
【问题描述】:

我正在尝试创建一个查找表,以便轻松创建具有不同值的对象。为此,我需要一个静态 std::array 在我的类中填充数据。 目前看起来是这样的:

#include <iostream>

#include <array>
#include <string>

struct MyStruct{
    std::string s;
    int a;
    int b;
};

class Arr{
public:
    static constexpr std::array<MyStruct, 3> strArray{{{"a", 1,2}, {"b", 2,3}, {"c", 3,4}}};
};

constexpr std::array<MyStruct, 3> Arr::strArray;

int main()
{    
    for(auto i : Arr::a){
        std::cout << i << std::endl;
    }

    std::cout << "With a struct:\n";
    for(auto i : Arr::strArray){
        std::cout << i.a << ", " << i.b << std::endl;
    }



    return 0;
}

如果我删除 std::string,它可以正常工作,但是使用 std::string 我得到编译错误

../staticArray/main.cpp:15:46: error: constexpr variable cannot have non-literal type 'const std::array<MyStruct, 3>'
    static constexpr std::array<MyStruct, 3> strArray{{{"a", 1,2}, {"b", 2,3}, {"c", 3,4}}};
                                             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/array:137:16: note: 'array<MyStruct, 3>' is not literal because it has data member '__elems_' of non-literal type 'value_type [3]'
    value_type __elems_[_Size > 0 ? _Size : 1];
               ^
../staticArray/main.cpp:19:40: error: constexpr variable cannot have non-literal type 'const std::array<MyStruct, 3>'
constexpr std::array<MyStruct, 3> Arr::strArray;
                                       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/array:137:16: note: 'array<MyStruct, 3>' is not literal because it has data member '__elems_' of non-literal type 'value_type [3]'
    value_type __elems_[_Size > 0 ? _Size : 1];

【问题讨论】:

  • 该错误表明non of the string constructors are constexpr.它们怎么可能适用于进行动态内存分配的类型? Your table cannot be constexpr if you want to use std::string.
  • @StoryTeller:请不要在 cmets 部分回答。使用答案部分。这就是为什么它被称为答案部分。因为..这是为了答案。
  • @luk32:从技术上讲,但那个不是很好。
  • @BoundaryImposition - 我正在寻找一个 dup(我相信它存在)。并不意味着我无法解决 OP 思维中的错误。
  • @BoundaryImposition 也许应该改进/给出更好的答案。本质上是一样的。虽然这更多是关于构造函数。不是size() 方法。

标签: c++ arrays c++11 static


【解决方案1】:

(从 cmets 部分迁移)

错误表明[none] of the string constructors [are] constexpr.它们怎么可能适用于进行动态内存分配的类型? Your table cannot be constexpr if you want to use std::string. (用户StoryTeller)

【讨论】:

    【解决方案2】:

    在 C++17 中,您可以使用 std::string_view 代替 std::stringhttp://coliru.stacked-crooked.com/a/946c48ee9f87a363

    由于某种原因,虽然你不能使用只接受 const char* 的构造函数(在 string_view 中)(应该可以,因为它是 constexpr)所以它也需要传递长度。

    在 C++11 中也可以这样做,但您必须自己创建 std::string_view 类似的类

    【讨论】:

    • “由于某种原因,虽然你不能使用只接受 const char* 的构造函数(我应该可以,因为它是 constexpr)” - No it isn't
    • @StoryTeller 我正在谈论abot string_view
    • 你可以使用 std::string_view_literals 来声明字面量{"a"sv, 1, 2}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多