g++ - how do I disable implicit conversion from 0 to pointer types?

Hristo Venev

Specifically, I want the following code to fail:

void a(void*){}
int main(){
    a(0); // FAIL
    a(NULL); // FAIL
    a(nullptr); // success
}

And I want the following code to compile:

void a(int){}
void a(void*){}
int main(){
    a(0); // calls first a
    a(NULL); // calls first a; that's why I have -Werror
    a(nullptr); // calls second a
}

The following code does not compile currently, but should according to my rule:

void a(std::size_t){}
void a(void*){}
int main(){
    a(0); // two candidates
}

Any idea how to make g++ behave like that?

firda

This may not be perfect, but if you trully want to have overloads with int and pointer, you could use some helper class like this:

#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;

template<typename T = void> class ptr {
    T* it;
public:
    ptr(T* it = nullptr): it(it) {}
    ptr(const ptr<T>&) = default;
    ptr& operator = (const ptr<T>&) = default;
    operator T* () { return it; }
    T& operator * () { return *it; }
    T* operator -> () { return it; }
    ptr& operator += (int x) { it += x; return *this; }
    ptr& operator -= (int x) { it -= x; return *this; }
    ptr& operator ++ () { ++it; return *this; }
//  etc...
public:
    template<typename P>
      ptr(P* it): it(it) {}
    template<typename P>
      ptr(ptr<P> it): it((T*)it) {}
};
template<> class ptr<void> {
    void* it;
public:
    ptr(void* it = nullptr): it(it) {}
    ptr(const ptr<void>&) = default;
    ptr& operator = (const ptr<void>&) = default;
    operator void* () { return it; }
public:
    template<typename P>
      ptr(P* it): it(it) {}
    template<typename P>
      ptr(ptr<P> it): it((void*)it) {}
};

void a(std::size_t x) {
    cout << "first: " << x << endl; }
void a(ptr<const int> p) {
    cout << "second: " << (p ? *p : -1) << endl; }
void a(ptr<int> p, ptr<> q) {
    cout << "third: " << (p ? *p : -1) << ", "
        << (q ? "some" : "null") << endl;
    a(p); }
int main(){
    a(0);           // first: 0
    a(NULL);        // first: 0 but warning [-Wconversion-null]
    a(new int(3), nullptr); // third: 3, null + second: 3
}

It is not finished (maybe remove that explicit, add more operators, special conversion from nullptr_t, etc), just and idea.

EDIT: Few changes in code, template constructors and conversion to ptr<const int> test.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do I get "no implicit conversion of String into Integer (TypeError)"?

From Dev

How can I prevent implicit conversion from char to int?

From Dev

How to solve Keychain error: Implicit conversion of C pointer type 'CFTypeRef'

From Dev

How to restrict implicit conversion of typedef'ed types?

From Dev

How do I prevent implict conversion to boost::variant types with multiple types of integers?

From Dev

Disable implicit conversion of quoted values to integer

From Dev

How do I disable Multibyte string input conversion in PHP?

From Dev

How to disable all implicit conversion of primitive types?

From Dev

Why is this implicit conversion (between different pointer types) valid?

From Dev

C++ implicit conversion of pointer type

From Dev

Revisiting reasons for no implicit conversion on smart pointer

From Dev

How to prevent implicit conversion from int to unsigned int?

From Dev

Extract return and argument types from a conversion operator to function pointer

From Dev

How to avoid implicit conversions from int(0) to pointer in a vector

From Dev

How to avoid implicit conversions from integer 0 to pointer, as the element of a vector

From Dev

Implicit conversion to pointer

From Dev

How do I permanently disable notifications about Auto capture keyboard and Mouse pointer integration for a Virtualbox VM?

From Dev

How to solve Keychain error: Implicit conversion of C pointer type 'CFTypeRef'

From Dev

How do I prevent implict conversion to boost::variant types with multiple types of integers?

From Dev

How can i do the implicit conversion?

From Dev

Disable implicit conversion of quoted values to integer

From Dev

g++ - how do I disable implicit conversion from 0 to pointer types?

From Dev

How do I disable g++ displaying notes for errors?

From Dev

Revisiting reasons for no implicit conversion on smart pointer

From Dev

Why do I get a TypeError: no implicit conversion of symbol into Integer?

From Dev

How do I disable file types on Desktop in Linux Mint?

From Dev

How do I disable a checkbox when the value in another field is 0?

From Dev

How to instruct gcc to warn me about invalid function pointer conversion just as g++ would do?

From Dev

Implicit conversion from null

Related Related

  1. 1

    Why do I get "no implicit conversion of String into Integer (TypeError)"?

  2. 2

    How can I prevent implicit conversion from char to int?

  3. 3

    How to solve Keychain error: Implicit conversion of C pointer type 'CFTypeRef'

  4. 4

    How to restrict implicit conversion of typedef'ed types?

  5. 5

    How do I prevent implict conversion to boost::variant types with multiple types of integers?

  6. 6

    Disable implicit conversion of quoted values to integer

  7. 7

    How do I disable Multibyte string input conversion in PHP?

  8. 8

    How to disable all implicit conversion of primitive types?

  9. 9

    Why is this implicit conversion (between different pointer types) valid?

  10. 10

    C++ implicit conversion of pointer type

  11. 11

    Revisiting reasons for no implicit conversion on smart pointer

  12. 12

    How to prevent implicit conversion from int to unsigned int?

  13. 13

    Extract return and argument types from a conversion operator to function pointer

  14. 14

    How to avoid implicit conversions from int(0) to pointer in a vector

  15. 15

    How to avoid implicit conversions from integer 0 to pointer, as the element of a vector

  16. 16

    Implicit conversion to pointer

  17. 17

    How do I permanently disable notifications about Auto capture keyboard and Mouse pointer integration for a Virtualbox VM?

  18. 18

    How to solve Keychain error: Implicit conversion of C pointer type 'CFTypeRef'

  19. 19

    How do I prevent implict conversion to boost::variant types with multiple types of integers?

  20. 20

    How can i do the implicit conversion?

  21. 21

    Disable implicit conversion of quoted values to integer

  22. 22

    g++ - how do I disable implicit conversion from 0 to pointer types?

  23. 23

    How do I disable g++ displaying notes for errors?

  24. 24

    Revisiting reasons for no implicit conversion on smart pointer

  25. 25

    Why do I get a TypeError: no implicit conversion of symbol into Integer?

  26. 26

    How do I disable file types on Desktop in Linux Mint?

  27. 27

    How do I disable a checkbox when the value in another field is 0?

  28. 28

    How to instruct gcc to warn me about invalid function pointer conversion just as g++ would do?

  29. 29

    Implicit conversion from null

HotTag

Archive