小编Ale*_*xey的帖子

如何在clang :: VarDecl中获取变量名的位置

我正在使用clang 3.0库来分析C/C++代码,我需要获取变量声明的位置,我试过这段代码:

clang::VarDecl * vd = ...;
clang::SourceManager & srcMgr = ...;

clang::SourceRange loc = vd->getSourceRange();
clang::PresumedLoc locStart = srcMgr.getPresumedLoc(loc.getBegin());
clang::PresumedLoc locEnd = srcMgr.getPresumedLoc(loc.getEnd());
Run Code Online (Sandbox Code Playgroud)

但是locStartlocEnd指向声明变量的开头(和结尾)(带有类型,可能是初始化器).例如:

const char * ptr = 0;
^            ^ ^   ^
Run Code Online (Sandbox Code Playgroud)

locStart将指向第一个指针(^),而locEnd将指向最后一个指针.如何获取第二个和第三个指针的位置(仅限名称,没有类型和初始化)?

c++ clang

6
推荐指数
1
解决办法
2451
查看次数

这是未指定(未定义)的行为吗?

我读过这篇文章:未定义的行为和序列点,但我不知道,是否是UB.

请考虑以下示例:

#include <iostream>
class op {
public:
  explicit op(int x) {
    std::cout << "x:   " << x << std::endl;
  }

  op & operator + (const op & /* other */) {
    return *this;
  }
};

int main(int /* argc */, char * /* argv */ []) {
  int x = 0;
  op o = op(x++) + op(x++) + op(x++);

  std::cout << "res: " << x << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望这样的输出(或基于评估顺序的输出的一些排列):

x:   0
x:   1
x:   2 …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
2
解决办法
173
查看次数

为什么编译器在使用CRTP时看不到基类的方法

我有以下代码:

struct Iface
{
    virtual int Read() = 0;

    int Read(int x) {
        return Read() + x;
    }
};

template <typename Impl>
struct Crtp : public Iface
{
    virtual int Read() {
        return static_cast<Impl&>(*this).ReadImpl();
    }
    //using Iface::Read;
};

struct IfaceImpl : public Crtp<IfaceImpl>
{
    int ReadImpl() {
        return 42;
    }
};

int main()
{
    IfaceImpl impl;
    impl.Read(24); // compilation error

    Iface& iface = impl;
    iface.Read(24); // always compiles successfully
}
Run Code Online (Sandbox Code Playgroud)

msvc,gcc和clang都拒绝这个代码,他们找不到方法 Read(int x)

但是如果我using Iface::ReadCrtp我的代码中取消注释成功编译.

请注意,如果我参考Iface,我可以打电话 …

c++ inheritance crtp

5
推荐指数
1
解决办法
102
查看次数

标签 统计

c++ ×3

clang ×1

crtp ×1

inheritance ×1