简体   繁体   English

const void(*功能)

[英]const void (* to function)

Is there any difference between: 之间有什么区别:

void (* const algorithm)();

and

void const (* const algorithm)();

when dealing with const pointers to static methods? 在处理指向静态方法的const指针时?

I understand that it would make sense to use const if the pointer points to memory that should not be modified, in case of pointers to variables, as stated in this answer . 我知道,如果指针指向不应该修改的内存(如果指向变量的指针),则使用const是有意义的,如本答案所述 However, are function addresses not effectively constant during run-time anyway? 但是,函数地址在运行时是否始终没有有效地保持不变?

The reason I am asking this is, the second option as a function parameter does not work. 我要问的原因是, 第二个选项作为函数参数不起作用。

EDIT 编辑

Here is the code that does not compile. 这是无法编译的代码。

struct A {
    static void a() {}
};

void b(void const (* const callback)()) {}

int main() {
    b(&A::a); // No matching function for call to 'b'
}

The above example works, if function a() has a return type const void . 如果函数a()具有返回类型const void ,则上面的示例有效。

const after (or before) the return type applies to the return type. 返回类型之后(或之前)的const适用于返回类型。 There is no such thing as a "pointer to const function" as there is no such thing as a const function. 没有所谓的“ const函数指针”,因为没有所谓的const函数。 (Although, there is such thing as a pointer to const member function, as const member functions do exist. But there the constness applies to the object argument, not to the function itself. There the constness is expressed in the same way as in the declaration of a member function - after the parenthesized parameter list) . (尽管存在const成员函数,但存在指向const成员函数的指针之类的东西。但是constness适用于object参数,而不适用于函数本身。constness的表达方式与成员函数的声明-在带括号的参数列表之后)

There is no difference between void() and void const() in the sense that both functions behave exactly the same because there is no behavioral difference between a const void return type and a non-const void return type. 在这两个函数的行为完全相同的意义上, void()void const()之间没有区别,因为const void返回类型和非const void返回类型之间没有行为差异。 Object cannot be const when there is no object. 没有对象时,对象不能为const。

I would expect a compiler to issue a warning when a non-class return value is directly qualified with const. 我希望当非类返回值直接用const限定时,编译器会发出警告。

However, void() and void const() are different in the sense that void and void const are technically separate types, as are all const qualified types different from their non-const counterpart. 但是, void()void const()在意义上是不同的,因为voidvoid const在技​​术上是分开的类型,所有const限定类型都不同于它们的非const对应类型。 Therefore the function pointers to const returning and non-const returning functions are different function pointer types. 因此,指向const返回函数和非const返回函数的函数指针是不同的函数指针类型。 As such, the standard won't allow a function pointer to one type be bound to a function of another type. 因此,该标准不允许将指向一种类型的函数指针绑定到另一种类型的函数。

So, to fix your non-compiling code, simply replace void const which is nonsensical with void in the function pointer. 因此,要修复您的非编译代码,只需用函数指针中的void替换无意义的void const即可。

Start with the "inner" const : 从“内部” const

using void_function = void();
void (*p1)() = nullptr;        // similar to `void_function* p1 = nullptr;`
void (* const p2)() = nullptr; // similar to `void_function* const p2 = nullptr;`

p2 is constant whereas p1 is mutable. p2constantp1是可变的。

When moving const , as following: 移动const ,如下所示:

const void_function* p3 = nullptr; // or void_function const* p3; // -> const has no effect
void (const* p4)() = nullptr;      // Invalid syntax

There are no " const function" vs "mutable function". 没有“ const函数”和“可变函数”。

Now, looking at function return type: 现在,查看函数返回类型:

Types void () (function returning void ) and const void () (function returning const void !?) are different. 类型void () (函数返回void )和const void () (函数返回const void )类型不同。

Even if const void make no real sense, it is a valid type. 即使const void没有实际意义,它也是有效的类型。

It might make sense to return const object from function to disallow "direct" modification of the object: 从函数返回const对象以禁止对该对象进行“直接”修改可能是有意义的:

const std::string make_const_string();

make_const_string().push_back('*'); // Doesn't work
std::string s = make_const_string(); // OK, create mutable copy.

So to fix your code: 因此,要修复您的代码:

struct A {
    static void a() {}
};

void b(void const (* const callback)()) {}

int main() {
    b(&A::a); // No matching function for call to 'b'
}

You have to make &A::a match b 's argument type: 您必须使&A::a匹配b的参数类型:

  • static const void a() {}
  • void b(void (*const callback)())

I suggest second one as const void make no real sense. 我建议第二个作为const void没有实际意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此函数将&#39;const char *&#39;强制转换为&#39;void * const&#39;而不是&#39;const void *&#39; - Why does this function cast 'const char*' to 'void* const' and not 'const void*' 函数中void const *参数的用途 - Purpose of void const * argument in function 将std :: function作为参数传递给const void * - passing std::function as parameter with const void * 将void函数模板专门化为const char [N] - Specialize a void function template to a const char[N] 函数参数 - &gt; struct成员的void *和const void *的解决方案 - Solution to void* and const void* for function parameter -> struct member 当B从A继承时,std :: function &lt;void(const A&)&gt;不能转换为std :: function &lt;void(const B&)&gt; - std::function< void ( const A & )> can't cast to std::function< void ( const B & ) > when B inherits from A 将 void* 转换为 const void** - Convert void* to const void** 如何将`void(* fn)(const char *,...)`翻译成`std :: function`,反之亦然 - How to translate `void(*fn)(const char *, …)` to `std::function` and vice versa OpenCL enqueueWriteImage在C ++包装器中但在C函数中没有const void * ptr - OpenCL enqueueWriteImage no const void* ptr in C++ wrapper but in C function 匹配bool与const void * overload的函数的地址 - The address of a function matching a bool vs const void* overload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM