3

I have been using somewhat similar code to following many times in Embedded C.

#include <stdio.h>

int g_uArray[5] =
{ 1, 1, 2, 3, 5};

void* foo( int uIndex );

int main()
{
    int* uVar;
    uVar = foo( 2 );
    printf( "Value = %u\n", *uVar );
    return 0;
}

void* foo( int uIndex )
{
    return (void*) &g_uArray[uIndex];
}

The above code works perfectly when compiled with gcc but it throws an error when compiled with g++

invalid conversion from ‘void*’ to ‘int*’

for line

uVar = foo( 2 );

However it can be compiled by giving -fpermissive flag. Now, my question is why it is so critical that C++ gives the error (gcc -Wall doesn't even give warning). If I compile using -fpermissive will it create some runtime problem?

Harsh Panchal
  • 53
  • 1
  • 7

2 Answers2

5

In C, void pointers are very common, and throwing away type information is more common in general because it is more cumbersome to try to keep everything type-safe. Because void pointers are so common, having to constantly do the casting would be cumbersome and maybe even more error prone.

In C++, void pointers are much more rare. Features like inheritance and templates provide much better alternatives. Therefore, C++ can afford to be a little more strict about the usage of void pointers when they actually are used. C++'s notion of a void pointer is a little like a "pointer to any object", so converting any object pointer to a "pointer to any object" is completely safe. Going the other way isn't so safe though. It relies on the programmer to only do this when appropriate, but to help avoid accidents, an explicit cast is required.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
4

because C++ is strongly typed (unlike C which has some weak typing features, mostly because of the implicit type conversion it handles), and all type casting that may change the semantic of the value shall be made explicitly, like explained on that page: http://www.cplusplus.com/doc/tutorial/typecasting/.

zmo
  • 24,463
  • 4
  • 54
  • 90
  • "implicit casting" is rather "implicit type conversion", "coalescing" or "[type] promotion". Casting is explicitly explicit (pun intended). –  Jun 09 '13 at 20:05
  • 1
    Yes, C++ forces the programmer to explicitly state what the compiler could easily figure out for itself, and has no way to figure out whether or not the programmer got it wrong. In this case, strong typing encourages more errors, like many features of C++. – Lee Daniel Crocker Jun 09 '13 at 20:09
  • 1
    @LeeDanielCrocker And what's even worse, there will be C++ programmers who assume that C is a subset of C++ and will include superfluous casts everywhere, cluttering the code. –  Jun 09 '13 at 20:11
  • @LeeDanielCrocker you're being a such a troll! language features don't make more errors, programmers do! ;-) – zmo Jun 09 '13 at 20:12
  • @zmo He didn't suggest the features were *making* the errors. He wrote that they *encourage* them, which is something completely different (and right). –  Jun 09 '13 at 20:19