简体   繁体   English

constexpr if else 表达式中的“预期语句”

[英]"Expected a statement" in constexpr if else expression

I have a function test , which prints out the underlying type of an enum parameter:我有一个函数test ,它打印出枚举参数的基础类型:

enum class TestEnum : uint32_t
{

};

template<typename TEnum>
    void test(TEnum v)
{ // Line 12
    if constexpr (std::is_same_v<std::underlying_type_t<TEnum>,int8_t>)
        std::cout<<"int8"<<std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>,uint8_t>)
        std::cout<<"uint8"<<std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>,int16_t>)
        std::cout<<"int16"<<std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>,uint16_t>)
        std::cout<<"uint16"<<std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>,int32_t>)
        std::cout<<"int32"<<std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>,uint32_t>)
        std::cout<<"uint32"<<std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>,int64_t>)
        std::cout<<"int64"<<std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>,uint64_t>)
        std::cout<<"uint64"<<std::endl;
    else
        static_assert(false,"Unsupported enum type!");
}

int main(int argc,char *argv[])
{
    TestEnum e {};
    test<TestEnum>(e);
    return EXIT_SUCCESS;
}

The program compiles and runs fine in Visual Studio 2017 (with ISO C++17), however the last else is underlined in red with the following message:该程序在 Visual Studio 2017(使用 ISO C++17)中编译并运行良好,但最后一个else用红色下划线标出,并显示以下消息:

expected a statement期待一个声明

detected during instantiation of "void test(TEnum v) [with TEnum=TestEnum]" at line 12在第 12 行的“void test(TEnum v) [with TEnum=TestEnum]”实例化期间检测到

最后一个“else”用红色下划线表示的程序代码。

(I've tried using else constexpr instead of just else , but that doesn't seem to matter.) (我试过使用else constexpr而不是else ,但这似乎并不重要。)

If I remove the last else if -branch (the one checking for uint64_t ), the error disappears:如果我删除最后一个else if -branch(检查uint64_t 的那个),错误就会消失:

没有最后一个“else if”分支的程序代码,并且没有错误消息。

Is this a bug in Visual Studio, or am I doing something that I shouldn't?这是 Visual Studio 中的错误,还是我做了我不应该做的事情?

I am sure this is not actually the answer you expected but… this code我确定这实际上不是您期望的答案,但是……这段代码

enum class TestEnum : uint32_t
{

};

template<typename TEnum>
void test(TEnum v)
{ // Line 12
    if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, int8_t>)
    {
        std::cout << "int8" << std::endl;
    }
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, uint8_t>)
    {
        std::cout << "uint8" << std::endl;
    }
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, int16_t>)
    {
        std::cout << "int16" << std::endl;
    }
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, uint16_t>)
    {
        std::cout << "uint16" << std::endl;
    }
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, int32_t>)
    {
        std::cout << "int32" << std::endl;
    }
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, uint32_t>)
    {
        std::cout << "uint32" << std::endl;
    }
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, int64_t>)
    {
        std::cout << "int64" << std::endl;
    }
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, uint64_t>)
    {
        std::cout << "uint64" << std::endl;
    }
    else
    {
        static_assert(false, "Unsupported enum type!");
    }
}

int main(int argc, char *argv[])
{
    TestEnum e{};
    test<TestEnum>(e);
    return EXIT_SUCCESS;
} 

does not produce any warning不产生任何警告

在此处输入图片说明

however然而

enum class TestEnum : uint32_t
{

};

template<typename TEnum>
void test(TEnum v)
{ // Line 12
    if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, int8_t>)
        std::cout << "int8" << std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, uint8_t>)
        std::cout << "uint8" << std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, int16_t>)
        std::cout << "int16" << std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, uint16_t>)
        std::cout << "uint16" << std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, int32_t>)
        std::cout << "int32" << std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, uint32_t>)
        std::cout << "uint32" << std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, int64_t>)
        std::cout << "int64" << std::endl;
    else if constexpr (std::is_same_v<std::underlying_type_t<TEnum>, uint64_t>)
        std::cout << "uint64" << std::endl;
    else
        static_assert(false, "Unsupported enum type!");
}

int main(int argc, char *argv[])
{
    TestEnum e{};
    test<TestEnum>(e);
    return EXIT_SUCCESS;
}

produces the same message as in your first screen capture.产生与您的第一个屏幕截图相同的消息。 I know it's french but trust me it says the same.我知道这是法语,但相信我,它说的是一样的。

在此处输入图片说明

Just for the debate I never really understood why the norm still allows只是为了辩论,我从来没有真正理解为什么规范仍然允许

if(boolean) do;

while尽管

if(boolean) { do;}

does the job and with no ambiguity whatsoever.做这项工作,没有任何歧义。 Surely a dirty heritage of what Fortran 77 allowed.肯定是 Fortran 77 允许的肮脏遗产。 Frankly 40 years has past and we are not about to scream if we have to add two more characters... Well I am not...坦率地说,40 年已经过去了,如果我们必须再添加两个角色,我们不会尖叫……好吧,我不是……

it seems to be a bug in IntelliSense.这似乎是 IntelliSense 中的一个错误。 It's not related with uint64_t or any other type.它与uint64_t或任何其他类型无关。 Everything above 8 if/else branches start to produce this error. 8 个以上的 if/else 分支开始产生这个错误。 Feel free to report to Microsoft随时向 Microsoft 报告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM