问题描述
[[maybe_unused]]似乎不适用于goto
标签,但是在g ++上[[maybe_unused]]
可以有效并抑制警告。
标准怎么说?我可以假设其他编译器也发生这种情况吗? (如果标准没有这样说)。
#include <iostream>
using namespace std;
template<uint32_t C>
struct Fixed
{
static constexpr uint32_t Components = C;
constexpr uint32_t components() const { return Components; }
};
struct Variable
{
static constexpr uint32_t Components = 0;
uint32_t c = 3;
uint32_t components() const { return c; }
};
template<typename T>
void function(T &&t)
{
if constexpr (T::Components <= 1)
{
if constexpr (!T::Components) if (t.components() != 1) goto con;
cout << "1 component,fixed or not\n";
goto fin;
}
[[maybe_unused]] con: // here it is!
cout << "many components,fixed or not\n";
fin:
cout << "final jobs\n";
}
int main()
{
function(Fixed<1>{});
function(Variable{});
return 0;
}