mardi 31 juillet 2018

inferring width of most template objects, but being explit with others with respect to template function?

I've noticed that modern C++ (C++11 etc..) can sometimes infer the width of a template object when passed to a function without explicitly passing the width using angle braces when invoking the function.

But, what if I want a functions argument, "bitsets", to have the width inferred automatically as a function input, and the return value, "bitset", to be explicitly specified when invoking the function using angle braces or casing. Is it possible? Example:

#include <bitset>
using namespace std;

// expand or truncate bitset without sign extension
template<int N_NEW, int N_OLD> 
bitset<N_NEW>
bresize(const bitset<N_OLD>)
{
return bitset<N_NEW> {value.to_ullong() & ((1<<N_NEW)-1) }; 
}

For instance is it possible:

bitset<16> x {0xABCD};

// version explicit everything (Not what I want)
auto y2 = bresize<32, 16>(x); 


//Inference Attempt #1
auto  y1 = bresize<32>(x); 

//Inference Attempt #2
bitset<32> y3 = bresize(x);

//Inference Attempt #3
auto y4 = (bitset<32>)bresize(x);

return

}

I just want to understand what the rules are when inferring a template size parameter in the scenario stated above... the goal is infer as much as possible regarding the input size of the bitset but to be explicit about the output size.. possible in modern c++11?

Aucun commentaire:

Enregistrer un commentaire