小编Mar*_*ski的帖子

在C++ 11中查找运算符的规则

N3337,"工作草案,编程语言C++标准",在第13.3.1.2节中给出了以下示例,p.10:

struct A { };
void operator + (A, A);
struct B {
  void operator + (B);
  void f ();
};
A a;
void B::f() {
  operator+ (a,a);   // error: global operator hidden by member
  a + a;             // OK: calls global operator+
}
Run Code Online (Sandbox Code Playgroud)

但是,这只是一个注意事项:

注意:表达式中运算符的查找规则与函数调用中运算符函数名称的查找规则不同,如以下示例所示:

我的问题是标准中的哪个位置表明这是必须发生的事情,而不仅仅是用一个例子来说明?

据我所知,根据第13.3.1.2条,p.2,运算符表达式转换为运算符函数调用.那么为什么以及如何在上面的例子中有所不同呢?

编辑:

在调查问题之后,我认为我可能忽略了这一点.同一条款中的第3和第6页一起声明全局候选人和成员候选人在查找运算符时被认为是平等的(因此查找规则与说明不同).但是,我对这个问题的调查是由这个例子引起的,它以与GCC 4.8和Clang相同的方式编译:

struct X {};  struct Y {};

void operator+(X, X) { }
void operator+(X, Y) { }

void test() {
  void operator+(X, X);
  X x; Y y;

  x + …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

15
推荐指数
1
解决办法
506
查看次数

在一组记录上调整域大小会挂起

我有一个超图数据结构,有两个数组,一个用于边,一个用于顶点(类似于二分图).我有一个调整数组大小的问题,所以我尝试了简化的例子:

ar dom = {0..0};

var arr: [dom] int;

writeln(arr);

dom = {0..#2};

writeln(arr);

dom = {0..#1};

writeln(arr);

record Vertex {}
record Edge   {}

record Wrapper {
  type nodeType;
  type idType;
  var id: idType;
}

record NodeData {
  type nodeIdType;

  var ndom = {0..-1};
  var neighborList: [ndom] nodeIdType;

  proc numNeighbors() return ndom.numIndices;
  var lock$: sync bool = true;

  // This method is not parallel-safe                                                                                                                                                                                                                                           
  proc addNodes(vals) {
    lock$; // acquire lock                                                                                                                                                                                                                                                      
    neighborList.push_back(vals);
    lock$ = true; // release the lock                                                                                                                                                                                                                                           
  } …
Run Code Online (Sandbox Code Playgroud)

chapel

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

可以将类型实例化与值实例化分开吗?

在C++中,我可以在编译时实例化泛型类型,然后在运行时构造它:

struct Vertex {};
struct Edge   {};

template<typename NodeType, typename IdType>
struct Wrapper {
  IdType id;

  Wrapper(IdType id) : id{id} {};
};

int main() {
  using vertexWrapper = Wrapper<Vertex, int>;
  vertexWrapper v(3);
}
Run Code Online (Sandbox Code Playgroud)

变量清楚地分开,类型看起来/感觉不像值.我想在Chapel做类似的事情:

record Vertex {}
record Edge   {}

record Wrapper {
  type nodeType;
  type idType;   
  var id: idType;

  proc init(id) {
    this.id = id;
  }
}

type vertexWrapper = Wrapper(Vertex, int);

var v1 = 3: vertexWrapper;
Run Code Online (Sandbox Code Playgroud)

当我编译这段代码时,我得到:

chpl -o graph-add-inclusion --cc-warnings test.chpl
test.chpl:9: In initializer:
test.chpl:10: error: can't …
Run Code Online (Sandbox Code Playgroud)

chapel

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

标签 统计

chapel ×2

c++ ×1

c++11 ×1