I try an official implementation of nullptr
I know with nullptr and std::nullptr_t supported by Compilers, this implementation is meaningless.I am just trying to study C++.
and everything works well by GCC4.9.1 on my PC, both -std=c++03 and -std=c++11, even successful Online @ideone.com GCC4.3.2.
but in VS2013 line 125 goes error2440, conversion failed
error C2440: "initialize": cannot convert "const <unnamed-type-my_nullptr>" to "int (__thiscall Test::* )(void) const" in line125
>
#include <iostream>
#include <cassert>
// The Official proposal
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf
const // this is a const object...
class {
public:
template<class T> // convertible to any type
operator T*() const // of null non-member
{
return 0;
} // pointer...
template<class C, class T> // or any type of null
operator T C::*() const // member pointer...
{
return 0;
}
private:
void operator&() const; // whose address can't be taken
} my_nullptr = {}; // and whose name is nullptr
struct Test123 {
int ABCD, ABC, AB;
int A;
int BA, CBA, DCBA;
struct XYZ {
char X, Y, Z, W;
};
struct {
int B, C, D;
};
} test123;
class Test {
int ABCD;
public:
void method(void){}
Test(int val) :ABCD(val){}
int getABCD(void) const { return ABCD; }
int get2ABCD(void) const { return ABCD * 2; }
int get3ABCD(void) const { return ABCD * 3; }
} test(0123);
int main(int argc, char* argv[])
{
// TEST_my_nullptr
{
int a = 321, *p = my_nullptr;
assert(p == my_nullptr);
assert(my_nullptr == p);
p = &a; *p = 0123;
assert(p != my_nullptr);
assert(my_nullptr != p);
p = my_nullptr;
int(*mainptr)(int argc, char** argv) = my_nullptr;
assert(mainptr == my_nullptr);
assert(my_nullptr == mainptr);
}
// TEST_my_nullptr_const
{
const int a = 321, *p = my_nullptr;
assert(p == my_nullptr);
assert(my_nullptr == p);
p = &a;
assert(p != my_nullptr);
assert(my_nullptr != p);
const int** ptr = my_nullptr;
assert(ptr == my_nullptr);
assert(my_nullptr == ptr);
ptr = &p;
assert(ptr != my_nullptr);
assert(my_nullptr != ptr);
assert(*ptr != my_nullptr);
assert(my_nullptr != *ptr);
}
// TEST_my_nullptr_member
{
int Test123::*pINT = my_nullptr;
assert(pINT == my_nullptr);
assert(my_nullptr == pINT);
pINT = &Test123::ABCD;
assert(pINT != my_nullptr);
assert(my_nullptr != pINT);
test123.*pINT = 0123;
const int Test123::*pCINT = my_nullptr;
assert(pCINT == my_nullptr);
assert(my_nullptr == pCINT);
pCINT = &Test123::ABCD;
assert(pCINT != my_nullptr);
assert(my_nullptr != pCINT);
assert(test123.*pCINT == test123.*pINT);
}
// TEST_my_nullptr_Function
{
void (Test::*pm)(void) = &Test::method;
pm = my_nullptr;
> int (Test::*pABCD)(void) const = my_nullptr; // This Fxxk Line In VS2013
pABCD = &Test::getABCD;
int a1 = (test.*pABCD)();
pABCD = &Test::get2ABCD;
int a2 = (test.*pABCD)();
pABCD = &Test::get3ABCD;
int a3 = (test.*pABCD)();
assert(a1 + a2 == a3);
}
std::cout << "All Tests for my_nullptr Passed!" << std::endl;
return 0;
}
my Testsuit in GCC4.3.2
This is probably a bug in VS2013. The bug seems to have been fixed in the VS14 CTP however, where your code compiles without errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With