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
, and checker
are two unrelated objects. The value 1
written to i
by the constructor has no bearing on the contents of checker
which remain uninitialized.
Perhaps your intent was to make a union?
精彩评论