jeudi 26 juillet 2018

c++: the possibility of using std::pair in switch

I wrote example. I want to use the std::pair in switch condition, but compiler says me that:

0) ‘t’ used in its own initializer;

1) "the value of ‘t’ is not usable in a constant expression"

Is it possible to make something like this or is it stupid and I need to go read the standard. Best regards.

#include <iostream>
#include <functional>
using namespace std;

bool Task() {
    std::cout<<"Hello Stack!" <<std::endl;
}


template<typename T>
void check_case(const uint8_t key, T val) {
    constexpr auto t =  std::pair<std::function<bool()>, uint8_t>([=] { return Task(); }, 0);
    switch (val) {
        case 0: {
            std::cout << "val are uint8" << std::endl;
        }break;
        case t: {
            std::cout << "val are pair" << std::endl;
        }break;
    }
}


int main() {
    uint8_t key = 0;
    uint8_t val = 0;
    check_case(key, val);

    auto t =  std::pair<std::function<bool()>, uint8_t>([=] { return Task(); }, 0);
    check_case(key, t);

    return 1;
}

Aucun commentaire:

Enregistrer un commentaire