decltype中的成员函数调用

发布于 2024-10-20 04:36:12 字数 464 浏览 10 评论 0原文

以下代码:

struct A
{
    int f(int);
    auto g(int x) -> decltype(f(x));
};

无法编译并出现错误:

error: cannot call member function 'int B::f(int)' without object

如果我将其更改为:

struct A
{
    int f(int);
    auto g(int x) -> decltype(this->f(x));
};

我收到另一个错误:

error: invalid use of 'this' at top level

其中任何一个有什么问题?我正在使用海湾合作委员会4.6

The following code:

struct A
{
    int f(int);
    auto g(int x) -> decltype(f(x));
};

Fails to compile with the error:

error: cannot call member function 'int B::f(int)' without object

If I change it to:

struct A
{
    int f(int);
    auto g(int x) -> decltype(this->f(x));
};

I get another error:

error: invalid use of 'this' at top level

What is wrong with either of these? I am using gcc 4.6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

把梦留给海 2024-10-27 04:36:12

以下是神奇的词语:

struct A
{
    int f(int);
    auto g(int x) -> decltype((((A*)0) ->* &A::f)(x)) ;
};

编辑 我从 Mikael Persson 的回答中看到,这就是 boost 中的实现方式。

Here are the magic words:

struct A
{
    int f(int);
    auto g(int x) -> decltype((((A*)0) ->* &A::f)(x)) ;
};

Edit I see from Mikael Persson's answer that this is how it's done in boost.

层林尽染 2024-10-27 04:36:12

目前,您只能访问“this”和函数体内的类成员,但这可能很快就会改变:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1207

Currently you can only access 'this' and members of the class inside the function body, but this is likely to be changed soon:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1207

长安忆 2024-10-27 04:36:12

result_of 和 decltype 组合可以给出成员函数的返回类型

#include <type_traits>
using namespace std;

struct A
{
    int f(int i) { return i; } 
    auto g(int x) -> std::result_of<decltype(&A::f)(A, int)>::type
    { 
        return x;
    }
};


int main() {
    A a;
static_assert(std::is_same<decltype(a.f(123)), 
                  decltype(a.g(123))>::value, 
                  "should be identical");
return 0;
}

result_of and decltype in combination can give the the return type for a member function

#include <type_traits>
using namespace std;

struct A
{
    int f(int i) { return i; } 
    auto g(int x) -> std::result_of<decltype(&A::f)(A, int)>::type
    { 
        return x;
    }
};


int main() {
    A a;
static_assert(std::is_same<decltype(a.f(123)), 
                  decltype(a.g(123))>::value, 
                  "should be identical");
return 0;
}
握住你手 2024-10-27 04:36:12

Comeau 不喜欢将 auto 作为顶级返回类型,但是以下编译成功:

template <typename R, typename C, typename A1> R get_return_type(R (C::*)(A1));

struct A
{
    int f(int);
    decltype(get_return_type(&A::f)) g(int x);
};

基本上,您必须声明至少一个附加构造来获得您想要的类型。并直接使用decltype

编辑:顺便说一句,这也适用于深入研究成员函数的返回类型:

template <typename R, typename C, typename A1> R get_return_type(R (C::*)(A1));

struct B { int f(int); };

struct A
{
    int f(int);
    B h(int);

    decltype(get_return_type(&A::f)) g(int x);

    decltype(get_return_type(&A::h).f(0)) k(int x);
};

int main()
{
    return A().k(0);
}

当然,它没有 auto f()-> 那样的便利性。 ...,但至少它可以编译。

Comeau doesn't like auto as a top level return type, but the following compiles successfully:

template <typename R, typename C, typename A1> R get_return_type(R (C::*)(A1));

struct A
{
    int f(int);
    decltype(get_return_type(&A::f)) g(int x);
};

Basically, you have to declare at least one additional construct that gets you the type you want. And use decltype directly.

EDIT: Incidentally, this works fine for diving into the return type of a member function as well:

template <typename R, typename C, typename A1> R get_return_type(R (C::*)(A1));

struct B { int f(int); };

struct A
{
    int f(int);
    B h(int);

    decltype(get_return_type(&A::f)) g(int x);

    decltype(get_return_type(&A::h).f(0)) k(int x);
};

int main()
{
    return A().k(0);
}

Granted, it doesn't have the same convenience of auto f()-> ..., but at least it compiles.

独闯女儿国 2024-10-27 04:36:12

经过一些测试,decltype(declval().f(x))decltype(((A*)0)->f(x)) 会起作用。

然而,似乎使用 boost::bind 可以工作(并且它是“幕后”版本):

struct A
{
    int f(int);
    auto g(int x) -> decltype(boost::bind(&A::f,0,x)());
    auto h(int x) -> decltype((((A*)0)->*(&A::f))(x)); //similarly (what Boost.Bind does under-the-hood.
};

当然,这并不漂亮。我想您可以研究一下 boost::bind 是如何做到的,也许会找到更好的解决方案。

编辑

正如MSN建议的,您还可以制作自己的函数模板来解决此问题:

template< typename R, typename C, typename... Args > R member_func(R (C::*)(Args...)); 

struct A
{
    int f(int);
    auto g(int x) -> decltype(member_func(&A::f));
};

After some tests, neither decltype(declval<A>().f(x)) nor decltype(((A*)0)->f(x)) will work.

However, it seems that using boost::bind will work (and it's "under-the-hood" version):

struct A
{
    int f(int);
    auto g(int x) -> decltype(boost::bind(&A::f,0,x)());
    auto h(int x) -> decltype((((A*)0)->*(&A::f))(x)); //similarly (what Boost.Bind does under-the-hood.
};

Of course, this is not pretty. I guess you can look into how boost::bind does it to maybe find a nicer solution.

EDIT

As MSN suggested, you can also make your own function template to resolve this:

template< typename R, typename C, typename... Args > R member_func(R (C::*)(Args...)); 

struct A
{
    int f(int);
    auto g(int x) -> decltype(member_func(&A::f));
};
南巷近海 2024-10-27 04:36:12

在我看来,这不起作用,因为 decltype 位于方法之外,而 A 此时是一个不完整的类型(因此您甚至无法执行 A().f(x))。

但你不应该真正需要它。在 A 的声明之外,这将按预期工作,在 A 中,您应该知道上面几行声明的函数的返回类型。或者你可以直接写:

struct A {
    typedef int ret_type;
    ret_type f(int x);
    ret_type g(int x);
};

这甚至适用于普通的 c++03。

Seems to me that does not work because the decltype is outside of the method and A is at that moment an incomplete type (so you can't even do A().f(x)).

But you should not really need that. Outside of the declaration of A this will work as expected, in A you should know the return type of the function that you declared a few lines above. Or you could just write:

struct A {
    typedef int ret_type;
    ret_type f(int x);
    ret_type g(int x);
};

This even works with plain c++03.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文