地图的Operator []无法使用作用域枚举作为键进行编译,但是at函数起作用为什么含糊不清?

问题描述

我有如下代码:

enum class MyTypes {
   TYPE_1 = 1,TYPE_2 = 2,TYPE_3 = 3
};

static const std::regex reg1("some unique expression");
static const std::regex reg2("some unique expression");
static const std::regex reg3("some unique expression");

static const std::map<MyTypes,std::regex> ValidSets = {
    {MyTypes::TYPE_1,reg1},{MyTypes::TYPE_2,reg2},{MyTypes::TYPE_3,reg3}
};

static const auto match = [&](const std::string& str,const std::regex& reg) -> bool {
    std::smatch base_match;
    return std::regex_match(str,base_match,reg);
};

template<MyTypes TYPE = MyTypes::TYPE_2> // defaulting to a specific type
class Foo {
    std::string sequence_{""};
public:
    const std::uint16_t Type = static_cast<uint16_t>(TYPE);         

    Foo() = default;
    Foo(const std::string_view sequence) {
        assert(!sequence.empty() && "invalid input,must contain at least 1 character.");
        // Fails to compile
        assert(match(sequence.data(),ValidSets[TYPE]) && "invalid sequence set for this type."); 
        // Works
        assert(match(sequence.data(),ValidSets.at(TYPE)) && "invalid sequence set for this type.");
        sequence_ = sequence;
    }
};

然后我将尝试按以下方式使用它:

int main() {
    Foo bar_d("some character sequence"); // must match the regex for the default type

    Foo<Type::TYPE1> bar_1("some character sequence"); // must match the regex for type 1
    Foo<Type::TYPE3> bar_3("some character sequence"); // must match the regex for type 3
 
    return 0;
}

但是,ValidSets[TYPE]无法在Visual Studio 2017编译器错误C2678中进行编译。但是,当我将其更改为使用ValidSets.at(TYPE)时,它可以编译并执行得很好...

为什么一个不能编译而另一个可以工作?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...