How to enable implicit type conversion from non-const to const in C++?

Michael

Here's the essence of the code:

class DM
{
public:
    int something;
    DM() : something(0) { }
};

typedef DM * dm_t;
typedef dm_t & dm_ref_t;
typedef const DM * dm_const_t;
typedef dm_const_t & dm_cref_t;

int getSomething( dm_cref_t dm ) // CALLING getSomething DOES NOT COMPILE
{
    return dm->something;
}

int getSomethingAgain( dm_const_t dm )
{
    return dm->something;
}

int getSomethingOnceMore( dm_ref_t dm )
{
    return dm->something;
}

int main()
{
    dm_t dm = new DM;
    getSomething( dm ); // COMPILER ERROR COMES FROM HERE
    getSomethingAgain( dm );
    getSomethingOnceMore( dm );
    return 0;
}

And here's the compiler error:

implicit_cast.cpp: In function ‘int main()’:
implicit_cast.cpp:31: error: invalid initialization of reference of type ‘const DM*&’ from expression of type ‘DM*’
implicit_cast.cpp:13: error: in passing argument 1 of ‘int getSomething(const DM*&)’

That is, I have a non-const pointer that I'd like to pass into a function that accept a const pointer by reference. All is fine and dandy if the function would accept a non-const pointer by reference or a const pointer by value, but const pointer by reference does not accept non-const pointer.

For safety reasons I'd like the function to take a const pointer; for some further development reasons I'd like it to be passed by reference. It should be completely safe to automatically cast from non-const to const. Why doesn't g++ performs the cast?

I'd like to proceed further with a function defined like getSomething. How can I legalize these calls without adding explicit casts every time I call it?

Bruno Ferreira

This is more like the C++ name "resolution". In your case, you have a pointer to a const DM, but you want a const pointer, so you must use typedef dm_const_t const & dm_cref_t (a const reference to a const pointer)

Giving more details:

When you declare const int * a, you are saying that a is a constant pointer to an integer. It is the same as int const * a.

If you want a reference to a type, you declare int & b, meaning that b is a reference to an integer. Thinking the same way, const int & c is the same as int const & c (const always qualify the variable, not the type).

So, if you want a const reference to a const pointer, using the second notation is clearer (and the only one possible if not using typedefs): int const * const & d, meaning d is a constant reference to a constant pointer to an integer. Using typedefs:

typedef const int * int_cptr;
typedef const int_cptr & int_cptrcref;

Proof: http://ideone.com/V48Kxe

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ invalid conversion from ‘const type* const’ to ‘type*’

From Dev

Why is an implicit conversion from non-const to const allowed for pointers in the first place?

From Dev

Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

From Dev

Automatic type conversion for templates with const/non-const pointer types

From Dev

conversion from `const char[2]' to non-scalar type `Persona' requested

From Dev

non-const reference of type from an rvalue

From Dev

non-const reference of type from an rvalue

From Dev

How to convert const to non const

From Dev

Implicit conversion to const string reference fails

From Dev

vector::push_back conversion from const Type* to type*

From Dev

c++: "invalid initialization of non-const reference of type ‘List&’ from a temporary of type ‘List*’"

From Dev

How implicit conversion works for non-type template parameters?

From Dev

Why does automatic conversion from const to non-const & occur when setting a class member?

From Dev

no viable conversion from returned value of type const_iterator to iterator

From Dev

no conversion from "std::allocator" to "const allocator_type"

From Dev

invalid initialization of non-const reference of type ' ' from an rvalue of type ' '

From Dev

Is there an implicit conversion from std::shared_ptr<T> to std::shared_ptr<const T>?

From Dev

invalid initialization of non-const reference of type cost char*& from a temporary of type const char*

From Dev

No conversion from "const char" to "int"

From Dev

No conversion from DWORD to const char*

From Dev

conversion from `const char*' to `byte'

From Dev

Where to put implicit value to enable implicit conversion about singleton type

From Dev

Const and non-const c++ constructor

From Dev

C++ getter function : const and non const

From Dev

How to check if a variable is of "const" qualifier type in C?

From Dev

Implicit integer type conversion in C

From Dev

How to specialize a template for const and non const containers?

From Dev

C++ invalid conversion from ‘char*’ to ‘const unsigned char*’

From Dev

C++: invalid conversion from 'const char*' to 'size_t'?

Related Related

  1. 1

    C++ invalid conversion from ‘const type* const’ to ‘type*’

  2. 2

    Why is an implicit conversion from non-const to const allowed for pointers in the first place?

  3. 3

    Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

  4. 4

    Automatic type conversion for templates with const/non-const pointer types

  5. 5

    conversion from `const char[2]' to non-scalar type `Persona' requested

  6. 6

    non-const reference of type from an rvalue

  7. 7

    non-const reference of type from an rvalue

  8. 8

    How to convert const to non const

  9. 9

    Implicit conversion to const string reference fails

  10. 10

    vector::push_back conversion from const Type* to type*

  11. 11

    c++: "invalid initialization of non-const reference of type ‘List&’ from a temporary of type ‘List*’"

  12. 12

    How implicit conversion works for non-type template parameters?

  13. 13

    Why does automatic conversion from const to non-const & occur when setting a class member?

  14. 14

    no viable conversion from returned value of type const_iterator to iterator

  15. 15

    no conversion from "std::allocator" to "const allocator_type"

  16. 16

    invalid initialization of non-const reference of type ' ' from an rvalue of type ' '

  17. 17

    Is there an implicit conversion from std::shared_ptr<T> to std::shared_ptr<const T>?

  18. 18

    invalid initialization of non-const reference of type cost char*& from a temporary of type const char*

  19. 19

    No conversion from "const char" to "int"

  20. 20

    No conversion from DWORD to const char*

  21. 21

    conversion from `const char*' to `byte'

  22. 22

    Where to put implicit value to enable implicit conversion about singleton type

  23. 23

    Const and non-const c++ constructor

  24. 24

    C++ getter function : const and non const

  25. 25

    How to check if a variable is of "const" qualifier type in C?

  26. 26

    Implicit integer type conversion in C

  27. 27

    How to specialize a template for const and non const containers?

  28. 28

    C++ invalid conversion from ‘char*’ to ‘const unsigned char*’

  29. 29

    C++: invalid conversion from 'const char*' to 'size_t'?

HotTag

Archive