简体   繁体   English

C ++参数应按值传递,但编译器将其视为引用

[英]C++ argument should be passed by value, but compiler sees it as reference

So its very weird behaviour which I do not understand. 因此,这是我不理解的非常奇怪的行为。 I have: 我有:

template <typename T>
Node Node::apply() {
    return ptr->graph->derived_node(std::make_shared<T>(ptr->graph, this));
}
template <typename T>
Node apply(Node parent1, Node parent2){
    GraphInPtr graph = parent1.ptr->graph;
    return graph->derived_node(std::make_shared<T>(graph, parent1, parent2));
}
template <typename T>
Node apply(NodeVec parents){
    GraphInPtr graph = parents[0].ptr->graph;
    return graph->derived_node(std::make_shared<T>(graph, parents));
}

I also have this: 我也有这个:

Node gt(Node node1, Node node2){
        return apply<GreaterThan>(node1, node2);
}

However, this here compiles with an error: 但是,这在编译时会出现错误:

Node Node::gt(Node node) {
        return apply<GreaterThan>(Node(this), node);
}

The error is: 错误是:

error: no matching function for call to ‘metadiff::Node::apply(metadiff::Node, metadiff::Node&)’
         return apply<GreaterThan>(Node(this), node);
note: candidate is:
note: template<class T> metadiff::Node metadiff::Node::apply()
     Node Node::apply()

So for some reason it is trying to call the Node::apply() instead of the apply(Node node1, Node node2) , but I don't understand why? 因此由于某种原因,它试图调用Node::apply()而不是apply(Node node1, Node node2) ,但是我不明白为什么? And why it interpreted the node as Node& ? 为什么将节点解释为Node&

Interestingly enough this worked: 有趣的是,它起作用了:

Node Node::gt(Node node) {
//        return apply<GreaterThan>(Node(this), node);
        GraphInPtr graph = ptr->graph;
        return graph->derived_node(std::make_shared<GreaterThan>(graph, this, node));
    }

Nevertheless, I'm very curious what is going on?! 不过,我很好奇发生了什么事!

And why it interpreted the node as Node& ? 为什么将节点解释为Node&

That's a red herring. 那是一条红鲱鱼。 It's the compiler's way of showing the value category of the second argument (which is an lvalue, so & ). 这是编译器显示第二个参数的值类别的方法(它是一个左值,所以& )。


Your actual problem is that in a member function, the class member Node::apply hides the namespace-scope function templates of the same name. 您的实际问题是在成员函数中,类成员Node::apply隐藏了具有相同名称的名称空间范围函数模板。 If you want to call the latter, qualify the call: 如果要呼叫后者,请限定呼叫:

return mynamespace::apply<GreaterThan>(Node(this), node);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++重载了相等运算符。 我是否应该编写函数以接受通过引用或值传递的参数? - C++ overloading the equality operator. Should I write my function to accept argument passed by reference or value? c++ 编译器会按值优化参数到右值引用的参数吗? - c++ will compiler optimize argument by value to argument by rvalue reference? C ++:参数传递“通过引用传递” - C++: Argument Passing “passed by reference” 通过值传递的参数显示为在Visual Studio 2010,C ++中的调试器中通过引用传递 - Argument passed by value appears as passed by reference in the debugger in Visual Studio 2010, C++ C ++函数参数通过值传递 - C++ function argument passed by value C ++编译器错误:传递指针但编译器看到引用 - C++ compiler error: passing pointers but compiler sees references 为什么数组的大小通过C ++编译器已知的引用传递给函数? - Why is the size of an array passed to a function by reference known to the compiler in C++? 如果将 function 返回值传递给 function 引用参数,那么 C++ 会发生什么? - What happens in C++ if a function return value is passed to a function reference argument? 在 C++ 中向量是按值还是按引用传递给函数 - Are vectors passed to functions by value or by reference in C++ C ++ - char **作为函数参数传入,它应该是调用者分配还是被调用者? - C++ - char** passed in as function argument, should it be caller allocated or callee?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM