G
Guest
I would expect the output to be:
5
post increment
assign
5
This is the case for Visual c++ 6 AND g++ 3.4.2. However, for visual c++
7.1, I get:
6
post increment
assign
5
Any ideas? Is this undefined behaviour? I think its pretty well defined. But
in either case, its inconsistent for v7.1 between built-ins and udt's!
#include <cassert>
#include <iostream>
struct test
{
int me;
test(int mee):me(mee){}
test operator++(int)
{
test a(*this);
++me;
std::cout << "post increment" << std::endl;
return a;
}
operator int(){return me;}
test & operator=(const test & rhs)
{
if(&rhs!=this) std::cout << "assign" << std::endl;
this->me = rhs.me;
return *this;
}
};
template<typename T>
void doit()
{
T aaa(T(5));
aaa = aaa++;
std::cout << int(aaa) << std::endl;
}
int main()
{
doit<char *>();
doit<test>();
}
5
post increment
assign
5
This is the case for Visual c++ 6 AND g++ 3.4.2. However, for visual c++
7.1, I get:
6
post increment
assign
5
Any ideas? Is this undefined behaviour? I think its pretty well defined. But
in either case, its inconsistent for v7.1 between built-ins and udt's!
#include <cassert>
#include <iostream>
struct test
{
int me;
test(int mee):me(mee){}
test operator++(int)
{
test a(*this);
++me;
std::cout << "post increment" << std::endl;
return a;
}
operator int(){return me;}
test & operator=(const test & rhs)
{
if(&rhs!=this) std::cout << "assign" << std::endl;
this->me = rhs.me;
return *this;
}
};
template<typename T>
void doit()
{
T aaa(T(5));
aaa = aaa++;
std::cout << int(aaa) << std::endl;
}
int main()
{
doit<char *>();
doit<test>();
}