Lets say I have : (this class Is OK)
class CCheckSystemEndianess
{
private:
int i;
char checker[sizeof(int)];
public:
CCheckSystemEndianess() : i(1) { }
bool isLittleEndian() { return (checker[0] != 0); }
};
But this one acts strange
template <bool checker>
class CEndian
{
private:
template <typename T>
T swapEndianess (const T& value)
{
if (CCheckSystemEndianess().isLittleEndian() == checker)
{
return value;
}
.................
The main problem is that if I create CEndian different ways I get different results with CCheckSystemEndianess().isLittleEndian()
For ex.
CEndian<false> *p_endian = new CEndian<false>();
then this line becomes
CcheckSystemEndianess().isLittleEndian() = false
But if I create CEndian like this
std::unique_ptr<CEndian<false> > p_endian (new Cendian<false>());
then this line becomes
CcheckSystemEndianess().isLittleEndian() = true.
And yes If I create CCheckSystemEndianes inside CEndian class everything goes OK
template <typename T>
T swapEndianess (const T& value)
{
std::unique_ptr<CCheckSystemEndianess> endianCheck(newCCheckSystemEndianess);
checkSystemEndianess().isLittleEndian() both ways with smart and with raw pointer gets true….
I am confused there a bit ..
Edit:
Thanks to Cubbi for pointing out that CCheckSystemEndianess has to be union not class.
In CCheckSystemEndianess,
i, andcheckerare two unrelated objects. The value1written toiby the constructor has no bearing on the contents ofcheckerwhich remain uninitialized.Perhaps your intent was to make a union?