11

重複の可能性:
関数ポインターとデータ ポインターが C/C++ で互換性がないのはなぜですか?

dlsym の man ページでは、次のスニペットが提供されています。

       double (*cosine)(double);

       handle = dlopen("libm.so", RTLD_LAZY);
       /* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
          would seem more natural, but the C99 standard leaves    
          casting from "void *" to a function pointer undefined.  
          The assignment used below is the POSIX.1-2003 (Technical
          Corrigendum 1) workaround; see the Rationale for the    
          POSIX specification of dlsym(). */

       *(void **) (&cosine) = dlsym(handle, "cos");

関連する仕様ページを調べましたが、void ポインターから関数ポインターへの変換を許可しない理由がわかりません。すべてのタイプのポインターに対応するのに十分な大きさであるはずの void ポインターではありません。もしそうなら、なぜこのキャストを未定義のままにしておくのですか?

4

0 に答える 0