nullptr

Why does not std::nullptr_t work with std::cout in C++?

早过忘川 提交于 2019-12-23 09:39:28
问题 I learned about std::nullptr_t that is the type of the null pointer literal, nullptr . Then I made small program : #include <iostream> int main() { std::nullptr_t n1; std::cout<<n1<<endl; return 0; } Here, nullptr_t is data type and n1 is variable and I'm trying to print the value of variable. But, Compiler give an error: prog.cpp: In function 'int main()': prog.cpp:6:11: error: ambiguous overload for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std:

In function call, why doesn't nullptr match a pointer to a template object?

孤街浪徒 提交于 2019-12-22 04:31:08
问题 Here is an example of a code that works perfectly: #include<iostream> #include<vector> template< class D, template< class D, class A > class C, class A = std::allocator< D > > void foo( C< D, A > *bar, C< D, A > *bas ) { std::cout << "Ok!" << std::endl; } int main( ) { std::vector< int > *sample1 = nullptr; std::vector< int > *sample2 = nullptr; foo( sample1, sample2 ); return( 0 ); } In the code below, however, the compiler is unable to match std::vector< int >* with nullptr for the second

In function call, why doesn't nullptr match a pointer to a template object?

对着背影说爱祢 提交于 2019-12-22 04:31:02
问题 Here is an example of a code that works perfectly: #include<iostream> #include<vector> template< class D, template< class D, class A > class C, class A = std::allocator< D > > void foo( C< D, A > *bar, C< D, A > *bas ) { std::cout << "Ok!" << std::endl; } int main( ) { std::vector< int > *sample1 = nullptr; std::vector< int > *sample2 = nullptr; foo( sample1, sample2 ); return( 0 ); } In the code below, however, the compiler is unable to match std::vector< int >* with nullptr for the second

Why can't you take the address of nullptr?

余生颓废 提交于 2019-12-20 10:59:23
问题 In the C++11 standard, I don't understand the reason why taking the address of nullptr is disallowed whereas one is allowed to take the address of their own std::nullptr_t instances. Aside from the fact that nullptr is a reserved keyword, is there any designated reasoning for this decision? Simply because it amuses me, I attempted to circumnavigate this restriction with the following function: decltype(nullptr)* func(const decltype(nullptr) &nref) noexcept { return const_cast<decltype(nullptr

Why can't you take the address of nullptr?

ε祈祈猫儿з 提交于 2019-12-20 10:59:13
问题 In the C++11 standard, I don't understand the reason why taking the address of nullptr is disallowed whereas one is allowed to take the address of their own std::nullptr_t instances. Aside from the fact that nullptr is a reserved keyword, is there any designated reasoning for this decision? Simply because it amuses me, I attempted to circumnavigate this restriction with the following function: decltype(nullptr)* func(const decltype(nullptr) &nref) noexcept { return const_cast<decltype(nullptr

Is it safe to #define NULL nullptr?

空扰寡人 提交于 2019-12-20 09:46:38
问题 I have seen below macro in many topmost header files: #define NULL 0 // C++03 In all over the code, NULL and 0 are used interchangeably. If I change it to. #define NULL nullptr // C++11 Will it cause any bad side effect ? I can think of the only (good) side effect as following usage will become ill-formed; int i = NULL; 回答1: I have seen below macro in topmost header file: You shouldn't have seen that, the standard library defines it in <cstddef> (and <stddef.h> ). And, IIRC, according to the

Assigned `nullptr` to `bool` type. Which compiler is correct?

徘徊边缘 提交于 2019-12-19 06:25:08
问题 I have a following snippet of code that assigned nullptr to bool type. #include <iostream> int main() { bool b = nullptr; std::cout << b; } In clang 3.8.0 working fine. it's give an output 0 . Clang Demo But g++ 5.4.0 give an error: source_file.cpp: In function ‘int main()’: source_file.cpp:5:18: error: converting to ‘bool’ from ‘std::nullptr_t’ requires direct-initialization [-fpermissive] bool b = nullptr; Which compiler is correct? 回答1: From the C++ Standard (4.12 Boolean conversions) 1 A

Is NULL defined as nullptr in C++11?

懵懂的女人 提交于 2019-12-19 05:06:59
问题 Will C++11 implementations define NULL as nullptr ? Would this be prescribed by the new C++ standard? 回答1: From the horse's mouth C.3.2.4 Macro NULL [diff.null] 1/ The macro NULL, defined in any of <clocale> , <cstddef> , <cstdio> , <cstdlib> , <cstring> , <ctime> , or <cwchar> , is an implementation-defined C++ null pointer constant in this International Standard (18.2). It is up to each implementation to provide its own definition, gcc if I recall correctly defines it to __nullptr for which

What are the uses of the type `std::nullptr_t`?

有些话、适合烂在心里 提交于 2019-12-17 20:03:52
问题 I learned that nullptr , in addition to being convertible to any pointer type (but not to any integral type) also has its own type std::nullptr_t . So it is possible to have a method overload that accepts std::nullptr_t . Exactly why is such an overload required? 回答1: If more than one overload accepts a pointer type, an overload for std::nullptr_t is necessary to accept a nullptr argument. Without the std::nullptr_t overload, it would be ambiguous which pointer overload should be selected

Can I check a C++ iterator against null?

末鹿安然 提交于 2019-12-12 10:34:50
问题 I'm having trouble with vector iterators. I've read in a few places that checking for null iterators isn't possible, and that the usual way to check iterators is to check it against vector.end() after a search. So for example: vector< Animal* > animalList; vector<Animal*>::iterator findInList(const type_info& type) { // Loop through list of Animals, if Dog found, return iterator to it } auto it = findInList(typeid(Dog)); // With a pointer I can check if it's null, but with an iterator I have