Call of overloaded function is ambiguous although different namespace - c++

I do understand why the following would be a problem if no namespaces were used. The call would be ambiguous indeed. I thought "using stD::swap;" would define which method to use.
Why does it work for "int" but not a "class"?
#include <memory>
namespace TEST {
class Dummy{};
void swap(Dummy a){};
void sw(int x){};
}
namespace stD {
void swap(TEST::Dummy a){};
void sw(int x){};
class aClass{
public:
void F()
{
using stD::swap;
TEST::Dummy x;
swap(x);
}
void I()
{
using stD::sw;
int b = 0;
sw(b);
}
};
}
This is the error message:
../src/Test.h: In member function ‘void stD::aClass::F()’:
../src/Test.h:26:9: error: call of overloaded ‘swap(TEST::Dummy&)’ is ambiguous
swap(x);
^
../src/Test.h:26:9: note: candidates are:
../src/Test.h:17:6: note: void stD::swap(TEST::Dummy)
void swap(TEST::Dummy a){};
^
../src/Test.h:10:6: note: void TEST::swap(TEST::Dummy)
void swap(Dummy a){};
^
I thank you very much in advance for an answer.

This line is using argument dependent lookup
TEST::Dummy x;
swap(x);
So it will find both void stD::swap(TEST::Dummy) as well as void TEST::swap(TEST::Dummy) because x carries the TEST:: namespace.
In the latter case int b = 0; the variable b is not in a namespace, so the only valid function to call would be stD::sw due to your using statement.

Related

C++, conflicting between library function and inhertited class function [duplicate]

This question already has an answer here:
c++ member function hides global function
(1 answer)
Closed 5 months ago.
#include <iostream>
#include <unistd.h>
using namespace std;
class A{
public:
bool close(){ return true; }
};
class B: public A{
public:
void fun(){
(void) close(1);
}
};
int main() {
B b;
b.fun();
return 0;
}
In class B I want to call the function close(1) which is in library unistd.h. FYI: close(int fileDes) used to close the file descriptors in process.
So how to overcome the issue. I got the below error:
source.cpp: In member function 'void B::fun()':
source.cpp:11:27: error: no matching function for call to 'B::close(int)'
11 | (void) close(1);
| ^
source.cpp:6:14: note: candidate: 'bool A::close()'
6 | bool close(){ return true; }
| ^~~~~
source.cpp:6:14: note: candidate expects 0 arguments, 1 provided
So how to overcome the issue, so that It will call the function in unistd.h file.
In the scope of the member function fun the name close as an unqualified name is searched in the scope of the class
void fun(){
(void) close(1);
}
And indeed there is another member function close in the base class with such a name.
So the compiler selects this function.
If you want to use a function from a namespace then use a qualified name as for example
void fun(){
(void) ::close(1);
}

virtual method of header give errors during g++ compilation

I'm initiating to C++ and I'm struggling with a compiling problem
I have a source file "binomial.cpp" in which I define the methods of my classes :
#include "binomial.hpp"
using namespace std;
int Bernoulli::operator()(){
return (rand() < p*RAND_MAX) ? a : b;
};
int Binomial::operator()(){
int result(0);
for(int i(0);i<n;++i){
int a;
a = B();
result += a;
};
return result;
};
and a header file "binomial.hpp" where I declare all my classes :
#include <iostream>
#ifndef BINOMIAL
#define BINOMIAL
class RandVar {
virtual int operator()() =0;
};
struct Bernoulli : public RandVar {
Bernoulli(int a = -1,int b = 1, double p = 0.5) : a(a), b(b), p(p) {};
int operator()(){};
private:
int a,b;
double p;
};
class Binomial : public RandVar {
public:
Binomial(Bernoulli B, int n=2)
: B(B), n(n) {}
int operator()(){};
private:
Bernoulli B;
int n;
};
#endif
But when I try to compile that through g++ using the command : g++ -Wall binomial.cpp -o binomial those errors occur :
binomial.hpp: In member function ‘virtual int Bernoulli::operator()()’:
binomial.hpp:14:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.hpp: In member function ‘virtual int Binomial::operator()()’:
binomial.hpp:26:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.cpp: At global scope:
binomial.cpp:4:5: error: redefinition of ‘int Bernoulli::operator()()’
int Bernoulli::operator()(){
^~~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:14:6: note: ‘virtual int Bernoulli::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
binomial.cpp:8:5: error: redefinition of ‘int Binomial::operator()()’
int Binomial::operator()(){
^~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:26:6: note: ‘virtual int Binomial::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
I don't really know how to fix that so if someone can take some time to help a beginner it would be great !
You should replace both
int operator()(){};
with
int operator()();
in the header files. You meant to provide declarations, not definitions. To provide just a declaration (not provide the code right away), just drop the {}.

C++ template overload call

I am trying to implement an execution pattern which takes any function and executes it with its own conditions/preparations. Regardless of this being a useful thing to do, it just doesn't work. It seems i can't access the template overload of the "Execute"-function (called in "main").
Specifically: Why can't i call the overloaded template function of Execute?
This is the full program:
#include "stdafx.h"
#include <functional>
class TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction) = 0;
template<class C>
C Execute(std::function<C(void)> executeFunction) // template-overload of the abstract function which will implicitly call it
{
C ret;
Execute( // calls the abstract function with a lambda function as parameter
[ret, executeFunction](void) -> C // lambda declaraction
{ //
ret = executeFunction; // lambda body
}); //
return ret;
}
};
class ExampleExecutor : public TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction)
{
printf("executed before.");
executeFunction();
printf("executed after.");
}
};
void DoStuff() {}
int ReturnStuff() { return -5; }
int main()
{
ExampleExecutor executor;
executor.Execute(DoStuff);
int i = executor.Execute<int>(ReturnStuff); // Why does this not work? ERROR: "type name is not allowed"
getchar();
return 0;
}
Note: Visual Studio marks
Execute<int>(ReturnStuff) // "int" is marked as Error: type name is not allowed
The compilation puts out the error
"type 'int' unexpected"
Thanks to everyone willing to help!
ExampleExecutor::Execute is not overriding TransparentFunctionWrapper::Execute, and it is hiding it in the executor.Execute<int> call.
You must explicitly call TransparentFunctionWrapper::Execute, as it is hidden by ExampleExecutor::Execute. Here's a possible way of doing that:
int i = executor.TransparentFunctionWrapper::Execute<int>(ReturnStuff);
live example on coliru

std::bind2nd on an already bind1st std::function

I am trying to figure out how binders work. I am working on the example from HERE. So I decided to extend it a bit but I can't figure out what is wrong
namespace mine {
using std::bind1st;
using std::bind2nd;
using std::function;
};
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { std::cout << num_+i << '\n'; }
int num_;
};
int main()
{
using namespace mine;
// store a call to a member function
function<void(const Foo&, int)> f_add_display = &Foo::print_add;
Foo foo(314159);
f_add_display(foo, 1);
function<void(int)> foo_f_add_display = bind1st(f_add_display, foo);
foo_f_add_display(1);
// The problem is here <------------
function<void(const Foo&)> f_1_add_display = bind2nd(f_add_display, 1);
// f_1_add_display(foo);
}
The error message I am getting (from Intel CC, gdb is unintelligible)
Compilation finished with errors:
c++/4.7/backward/binders.h(160): error: invalid redeclaration of member function "std::binder2nd<_Operation>::operator()(const _Operation::first_argument_type &) const [with _Operation=std::function<void (const Foo &, int)>]" (declared at line 154)
operator()(typename _Operation::first_argument_type& __x) const
^
detected during instantiation of class "std::binder2nd<_Operation> [with _Operation=std::function<void (const Foo &, int)>]" at line 41 of "source.cpp"
compilation aborted for source.cpp (code 2)
What exactly is the problem here. Why is not possible to bind the second argument? or is it just some syntax error or something?
Code is HERE if anyone needs it.

Does a using-declaration only import overloads declared above the using-declaration?

For example, GCC and clang both fail to compile the following code:
struct S {};
namespace N
{
void g(S);
}
using N::g;
namespace N
{
void g(int);
}
int main()
{
g(0);
}
with the error:
test.cpp: In function 'int main()':
test.cpp:17:8: error: could not convert '0' from 'int' to 'S'
g(0);
^
suggesting that the using-declaration only imports the overloads declared above the point where the using-declaration appears, not ones that may appear later (but before the use of the name).
Is this behaviour correct?
Is this behaviour correct?
Yes, this behavior is correct and is well defined as per the c++ standard.
The relevant section is § 7.3.3.11 of C++11 standard:
The entity declared by a using-declaration shall be known in the context using it according to its definition at the point of the using-declaration. Definitions added to the namespace after the using-declaration are not considered when a use of the name is made.
[ Example:
namespace A {
void f(int);
}
using A::f; // f is a synonym for A::f;
// that is, for A::f(int).
namespace A {
void f(char);
}
void foo() {
f(’a’); // calls f(int),
} // even though f(char) exists.
void bar() {
using A::f; // f is a synonym for A::f;
// that is, for A::f(int) and A::f(char).
f(’a’); // calls f(char)
}
—end example ]