相关疑难解决方法(0)

为什么decltype没有看到成员声明?

试图编译这个简单的类:

#include <vector>
struct M
{
// interface
    auto begin() -> decltype(identities.begin())
    {
        return identities.begin();
    }
// implementation
private:
    std::vector<int> identities;
};
Run Code Online (Sandbox Code Playgroud)

导致错误:

$ g++-510 where.cpp -std=c++11
where.cpp:57:35: error: ‘struct M’ has no member named ‘identities’
     auto begin() ->decltype(this->identities.begin())
                                   ^
where.cpp:57:35: error: ‘struct M’ has no member named ‘identities’

$ clang++ where.cpp -std=c++11 -Wall -pedantic -Wextra
where.cpp:57:35: error: no member named 'identities' in 'M'
    auto begin() ->decltype(this->identities.begin())
                            ~~~~  ^
Run Code Online (Sandbox Code Playgroud)

为什么decltype看不到班级成员?

c++ c++11

27
推荐指数
2
解决办法
1288
查看次数

decltype作为类成员函数中的返回类型

我在代码下面编译错误.

struct B{
    double operator()(){
        return 1.0;
    }
};

struct A {
    auto func() -> decltype(b())
    {
        return b();
    }

    B b;
};
Run Code Online (Sandbox Code Playgroud)

但是,如果我重组A,它会编译.

gcc 4.8表示'b'未在此范围内声明.

struct A {
    B b;
    auto func() -> decltype(b())
    {
        return b();
    }
};
Run Code Online (Sandbox Code Playgroud)

那么,第一个出了什么问题?

c++ decltype c++11

12
推荐指数
2
解决办法
2164
查看次数

标签 统计

c++ ×2

c++11 ×2

decltype ×1