在case中用成员变量开关case语句 [英] Switch case statement with member variable in case

查看:186
本文介绍了在case中用成员变量开关case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来评估切换 - case 语句使用成员变量部分。我认为有一个全局静态变量如下所示将允许作为const表达式。



不幸的是,编译器告诉我相反:

 错误:'ll'不能出现在常量表达式中
错误:'。'不能出现在常量表达式中

有没有任何关键字或任何东西允许这个想法工作?
我不是 C ++ 11 的专家,我听说过 constexpr

 枚举MyEnum2 {A = 0,B = 1}; 

class Test {
public:
operator MyEnum2(){return val_;}
Test(MyEnum2 val):val_(val){}
Test(){}
static const MyEnum2 A;
static const MyEnum2 B;
MyEnum2 val_;
};

const MyEnum2 Test :: A(MyEnum2 :: A);
const MyEnum2 Test :: B(MyEnum2 :: B);

static const测试ll;

int main(){
class Test v = ll.A;
cout<< v<< endl;
switch(v){
case ll.A:
cout< A<< endl;
break;

case ll.B:
cout< B< endl;
break;
}
}


解决方案

元素是类的一部分,而不是实例的一部分。
所以你必须写:

  case Test :: A:

因为case表达式中的值必须是常量表达式,所以也可以使用
constexpr方法:

  class A 
{
public:
constexpr int X(){return 42; }
};



int main()
{
int i = 42;
A a;

switch(i)
{
case a.X():
;

}
}

编辑以回答问题: p>

您可以创建一个类的constexpr对象,该对象可以实例化为constexpr,只需要一个如下所示的constexpr构造函数:

  #include< iostream> 

using namespace std;

const int i = 9; //使用const变量作为编译时const

class Y //使用包含const vals的类
{
public:
int i;
constexpr Y(int _i):i(_i){}
};

constexpr Y y(100);

int main()
{
int var = 9;

switch(var)
{
case i:
;

case y.i:
;
}
}

但我可以看到没有任何真正的用例种类的编程。 switch语句不能与其他实例重用,因为你不能给开关表达式另一个对象的常量表现不同。所以你简单地以一种非常特殊的方式隐藏你的常量值,这对于别人来说可能不是那么好。



你能给我们你的用例吗? >

I am trying to find a way to evaluate a switch-case statement using a member variable in the case part. I thought that having a global static variable like below would be allowed as const-expression.

Unfortunately the compiler tells me the opposite:

error: ‘ll’ cannot appear in a constant-expression
error: ‘.’ cannot appear in a constant-expression

Is there any keyword or anything that would allow this idea to work? I am not an expert of C++11, and I heard about constexpr.

enum MyEnum2 {A=0, B=1};

class Test{
    public:
        operator MyEnum2 () { return val_;}
        Test(MyEnum2 val) :val_(val) {}
        Test() {}
        static const MyEnum2 A;
        static const MyEnum2 B;
        MyEnum2 val_;
};

const MyEnum2 Test::A(MyEnum2::A);
const MyEnum2 Test::B(MyEnum2::B);

static const Test ll;

int main() {
    class Test v = ll.A;
    cout << v << endl;
    switch(v) {
        case ll.A:
            cout << "A" << endl;
            break;

        case ll.B:
            cout << "B" << endl;
            break;
    }
}

解决方案

Static elements are parts of the class and not of the instance. So you have to write:

case Test::A:

Because the value in a case expression must be a constant expression, you can also use a constexpr method like this:

class A
{
    public:
        constexpr int X() { return 42; }
};



int main()
{
    int i=42;
    A a;

    switch (i)
    {
        case a.X():
            ;

    }
}

Edit to answer questions:

You can make a constexpr object of a class which can be instantiated as constexpr which simply needs a constexpr constructor like the following:

#include <iostream>

using namespace std;

const int i = 9;    // using a const variable as compile time const 

class Y             // using a class containing const vals
{
    public:
    int i;
    constexpr Y(int _i): i(_i){}
};

constexpr Y y(100);

int main()
{
    int var=9;

    switch (var)
    {
        case i:
            ;

        case y.i:
            ;
    }
}

But I can see not any real use case for this kind of programming. The switch statement can not be "reused" with an other instance, because you can not give a switch expression a other "object" of constants to behave different. So you simple hide your constant values in a very special way which is maybe not so nice for others to read.

Can you give us your use case please?

这篇关于在case中用成员变量开关case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆