vendredi 30 juin 2017

restrict function parameters to certain enum values

Here's my first attempt -

#include <iostream>
using namespace std;

enum class props {
    left, right
};

template<typename T>
auto allowLeftOnly(T p) -> decltype((p==props::left), void())
{
    cout << "Wow!";
}

int main() {
    props p1 = props::left;
    props p2 = props::right;
    allowLeftOnly(p1);
    // allowLeftOnly(p2);  // should fail to compile
}

what I want from allowLeftOnly function is to accept only props::left or others that I explicitly specify as parameters and fail to compile for others. Is that possible?

Aucun commentaire:

Enregistrer un commentaire