Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

c++ - Unable to print the value of nullptr on screen

I was reading about nullptr and doing workout on g++ and also on VS2010.

When I did

#include <iostream>
using namespace std;

auto main(void)->int
{
    int j{};    
    int* q{};   

    cout << "Value of j: " << j << endl; // prints 0
    cout << nullptr << endl;
    cout << "Value of q: " << q << endl; // prints 0

    return 0;
}

printing the value of nullptr on screen, g++ and VS gave compiler error. Is it not allowed to print the value of nullptr on screen?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t.

Type nullptr_t should be convertible to T*, but compiler has no operator << for nullptr_t and don't know to which type you want to convert nullptr.

You can use this

cout << static_cast<void*>(nullptr) << endl;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...