如果语句无法评估条件 [英] If statement failing to evaluate condition

查看:77
本文介绍了如果语句无法评估条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本类,其中包含两个枚举器,一个枚举用于输入,一个枚举用于输出。它具有两个都是静态的成员函数。第一个函数只是一个静态函数,它根据输入返回一个值。它将调用第二个函数,这是一个constexpr函数模板,它将返回constexpr值。

I have a basic class that containers two enumerators, one for input and one for output. It has two member functions which are both static. The first function is just a static function that returns a value based on the input. It will call the second function which is a constexpr function template that will return the constexpr values. You can see the full class here.

class Foo {
public:
    enum Input {
        INPUT_0 = 0,
        INPUT_1,
        INPUT_2
    };

    enum Output {
        OUTPUT_0 = 123,
        OUTPUT_1 = 234,
        OUTPUT_2 = 345
    };

    static uint16_t update( uint8_t input ) {
        if ( static_cast<int>(input) == INPUT_0 )
            return updater<INPUT_0>();
        if ( static_cast<int>(input) == INPUT_1 )
            return updater<INPUT_1>();
        if ( static_cast<int>(input) == INPUT_2 )
            return updater<INPUT_2>();

        return updater<INPUT_0>();
    }

    template<const uint8_t In>
    static constexpr uint16_t updater() {

        if constexpr( In == INPUT_0 ) {
            std::cout << "Output updated to: " << OUTPUT_0 << '\n';
            return OUTPUT_0;
        }

        if constexpr( In == INPUT_1 ) {
            std::cout << "Output updated to: " << OUTPUT_1 << '\n';
            return OUTPUT_1;
        }

        if constexpr( In == INPUT_2 ) {
            std::cout << "Output updated to: " << OUTPUT_2 << '\n';
            return OUTPUT_2;
        }
    }
};

如果在编译时知道值时使用函数模板本身:

If I use the function template itself as such when the values are known at compile time:

#include <iostream>

int main() {
    auto output0 = Foo::updater<Foo::INPUT_0>();
    auto output1 = Foo::updater<Foo::INPUT_1>();
    auto output2 = Foo::updater<Foo::INPUT_2>();

    std::cout << "\n--------------------------------\n";
    std::cout << "Output0: " << output0 << '\n'
              << "Output1: " << output1 << '\n'
              << "Output2: " << output2 << '\n';    

    return 0;
}

我得到正确的输出:

-输出-

Output updated to: 123
Output updated to: 234
Output updated to: 345

---------------------------------
Output0: 123
Output1: 234
Output2: 345






但是,当我在运行时确定值时尝试使用non constexpr成员函数时,由于某种原因或其他原因,non constexpr函数无法在其中执行代码


However when I try to use the non constexpr member function when the values are determined at runtime, for some reason or another the non constexpr function is failing to execute the code within the if statements.

#include <iostream>

int main() {
    uint8_t input;
    std::cout << "Please enter input value [0,2]\n";
    std::cin >> input;

    auto output = Foo::update( input );

    std::cout << "Output: " << output << '\n';

    return 0;        
}

无论我从键盘输入什么值, 0 1 2 ,则无法执行<$ c中的代码$ c> Foo :: update()的 if语句。它总是打印出 123 的值。

Regardless of what value I enter from the keyboard, 0, 1 or 2, it is failing to execute the code within Foo::update()'s if statements. It is always printing out a value of 123.

如果有帮助,我正在使用 Visual Studio 2017 CE v15.9.4 并使用设置为 ISO C ++最新草案标准(/ std:c ++最新)

If it helps; I'm using Visual Studio 2017 CE v15.9.4 and I'm compiling it with language set to ISO C++ Latest Draft Standard (/std:c++latest).

我不知道为什么这段代码无法评估 if语句为true,并在其范围内调用代码。

I don't know why this code is failing to evaluate the if statements to true and calling the code within their scope.

推荐答案

输入收到 char ,因此它将被设置为输入字符的ASCII值。例如。输入 2 会将 input 设置为50。

input is receiving a char, so it will be set to the ASCII value of the inputted character. E.g. entering 2 will set input to 50.

下一步时间,请使用调试器来确定您的程序逻辑误入何处。您本可以轻松地自己找到问题的解决方案。

Next time, use a debugger to identify where your program logic goes astray. You could have easily found the solution to your problem on your own.

这篇关于如果语句无法评估条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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