std::is_function专门用于具有与以下类似的签名的类型:
int(int) &见此处:函数
但这既不是指向成员方法的指针,该成员方法的签名可能是:
int(T::*)(int) &它也不能是对函数的引用:
int (&)(int)这个奇怪的签名是什么?
发布于 2017-06-26 18:51:22
它是一种只存在于类型系统中的函数类型。它永远不会被创造出来。
但这既不是指向成员方法的指针,该成员方法的签名可能是: int(T::*)(int) &
就是这个,没有指针。类型系统允许您将其描述为类型。
#include <type_traits>
struct T { };
using A = int(int) &;
using B = A T::*;
using C = int(T::*)(int) &;
static_assert(std::is_same_v<B, C>);@T.C.提到了PR0172R0,它讨论了这些类型的存在如何给库编写人员带来问题,并提出了一些可能减少这些问题的选项。其中一种选择是完全消除它们,其他方案则减少它们的影响。这取决于这是如何进行的,这个答案可能是正确的,也可能是不正确的未来版本的C++。
发布于 2017-06-26 18:54:56
在您链接到的文档页面上,您将看到以下评论:
// specialization for function types that have ref-qualifiers在上面的列表中,你引用的例子来自于。
这些都是带有ref-限定符的函数,您可以阅读更多关于这里的内容。
简而言之,它们类似于const限定函数。下面是一个例子:
struct foo
{
void bar() & { std::cout << "this is an lvalue instance of foo" << "\n"; }
void bar() && { std::cout << "this is an rvalue instance of foo" << "\n"; }
};
int main(int argc, char* argv[])
{
foo f{};
f.bar(); // prints "this is an lvalue instance of foo"
std::move(f).bar(); // prints "this is an rvalue instance of foo"
return 0;
}对于这个特性,我想不出一个很好的用例,但它是可以使用的。
发布于 2017-06-26 19:07:56
从一开始(参考第一个C++标准),您就可以声明这样的“奇怪”函数类型,例如
typedef int F() const;尽管上述声明并没有立即涉及任何类,但在这种情况下,尾随const只能作为非静态类成员函数的const限定。这限制了对类成员声明的使用。例如,您可以如下所示使用它
struct S {
F foo; // Declares an `int S::foo() const` member function
};
int S::foo() const { // Defines it
return 42;
}
F S::*p = &S::foo; // Declares 'p' as `int (S::*)() const` pointer请注意,不管多么晦涩,这是一个“经典”的C++特性,已经在语言中使用了很长时间。
示例中的内容实际上是相同的,但使用C++11 参-限定符代替const限定符。
https://stackoverflow.com/questions/44766581
复制相似问题