English 中文(简体)
在哪些情况下,标准禁止点人担任职能/目标? [复制]
原标题:In what cases does the standard forbid taking pointers to functions/objects? [duplicate]

This article lists all the different categories of pointers. I have tested the explicit conversion of different types of pointers to const void* in the following snippet (live):

#include <print>


int var {};

void foo()
{
}

struct Bar
{
    char mem;

    void func()
    {
    }
};

Bar b {};

int main()
{
    // nullptr
    std::println( "{}", static_cast<const void*>( nullptr ) );
    // pointer to object
    std::println( "{}", static_cast<const void*>( &var ) );
    // pointer to function
    std::println( "{}", reinterpret_cast<const void*>( &foo ) );
    // pointer to member variable
    std::println( "{}", static_cast<const void*>( &Bar::mem ) ); // doesn t compile
    std::println( "{}", static_cast<const void*>( &(b.mem) ) );
    std::println( "{}", static_cast<const void*>( &(b.*(&Bar::mem)) ) );
    // pointer to member function
    std::println( "{}", reinterpret_cast<const void*>( &Bar::func ) ); // compiles with a warning
    std::println( "{}", reinterpret_cast<const void*>( &(b.func) ) ); // doesn t compile
    std::println( "{}", reinterpret_cast<const void*>( &(b.*(&Bar::func)) ) ); // doesn t compile

    // Did I miss any other category?
}

现在我有几个问题:

  1. I want to know why static_cast<const void*>( &Bar::mem ) ); does not compile (since &(b.*(&Bar::mem)) does). The compiler says:
error: invalid  static_cast  from type  char Bar::*  to type  const void* 
  1. Also I want to know why it s not allowed to take the address of member functions (as seen in the last three statements). The compiler says:
ISO C++ forbids taking the address of a bound member function to form a pointer to member function.  Say  &Bar::func 
  1. Finally, which pointer types are we allowed to take (at least for trivial tasks like printing) without invoking errors or UB? It seems to me that pointer to member function is not allowed.
问题回答

一名成员与其他要点不同。 它不受特定情况的约束,而是被抵消为一类。 如果出现这样的情况,你可以向成员求助点。 将它列入<代码> 避免*是毫无用处的,因此C++不允许这样做。 总的说来,C++的宽度低于C组的排泄物,即<0> 代码/代码>。 因此,static_cast<const void*> &Bar:mem )>;不汇编。

另一方面,<条码>(b.*(&Bar:mem)使用条码,并请成员参考<条码>>>。 http://www.ohchr.org。 <代码>b.mem,char*。 页: 1

(2) 和(3) 只是 about子的定义。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?