mercredi 20 juin 2018

C++11 Template function to "implicity" convert bitset

I can create a C++11 Template function to explicitly convert a bitset into an unsigned long:

template<size_t N>
unsigned long U(bitset<N> x) {
    return x.to_ulong();
}

// Explicit conversion
unsigned long y = U(bitset<4>("1010"));

Is there a way to "implicitly" convert bitset to an unsigned long using a C++11 template helper function. For Example, if I assign a bitset to an unsigned long, then I want it to automatically convert the bitset to the correct type:

// What Template Function in C++11?

// Implicit Conversion
unsigned long z = bitset<4>("1010");

Aucun commentaire:

Enregistrer un commentaire