Explicitly choose which template overload should be used - c++

Can we chose which function template overload should be used in this case?
struct X { };
struct A { A(X){} };
struct B { B(X){} };
template<class T>
void fun(T, A) { }
template<class T>
void fun(T, B) { }
int main() {
/* explicitly choose overload */ fun(1, X());
}
Error:
error: call of overloaded 'fun(int, X)' is ambiguous
/* explicitly choose overload */ fun(1, X());
^
candidate: void fun(T, A) [with T = int]
void fun(T, A) { }
^~~
candidate: void fun(T, B) [with T = int]
void fun(T, B) { }
^~~
For normal function it looks like this:
void fun(A){}
void fun(B){}
int main() {
((void(*)(A))(fun))(X());
}
Is it possible?

Improving your example, you can try with
((void(*)(int, A))(fun))(1, X());

If you choose not to explicitly specify the first parameter type but still want to specify the second you could go along with lambda dedicated for casting purpose (c++14 solution):
struct X { };
struct A { A(X){} };
struct B { B(X){} };
template<class T>
void fun(T, A) { }
template<class T>
void fun(T, B) { }
int main() {
[](auto v, auto x){ static_cast<void(*)(decltype(v), A)>(fun)(v, x); }(1, X());
}
[live demo]

A low-tech solution to this problem would be to add one extra level of indirection. Add a function like funA, whose sole purpose is to give an explicit name to the first version of fun:
struct X { };
struct A { A(X){} };
struct B { B(X){} };
template<class T>
void fun(T, A) { }
template<class T>
void fun(T, B) { }
template <class T>
void funA(T t, A a) { fun(t, a); }
int main() {
/* explicitly choose overload */ funA(1, X());
}
However, I wonder why you cannot just change the argument to A(X()). You will have to change the calling code anyway, so what's the problem?

Related

C++20 concepts how to define existence of a function with arguments?

In C++20, we can now use concepts instead of SFINAE to figure out whether a function exists in a template typename:
template<typename T> concept fooable = requires (T a) {
a.foo();
};
class Foo {
public:
// If commented out, will fail compilation.
void foo() {}
void bar() {}
};
template <typename T> requires fooable<T>
void foo_it(T t) {
t.bar();
}
int main()
{
foo_it(Foo());
}
How do we do this with functions that have non-empty arguments?
You might have extra parameters in requires:
template<typename T> concept fooable = requires (T a, int i) {
a.foo(i);
};
Demo
The best option seems to be declval:
template<typename T> concept fooable = requires (T a) {
a.foo(std::declval<int>());
};
class Foo {
public:
void foo(int x) {}
void bar() {}
};
template <typename T> requires fooable<T>
void foo_it(T t) {
t.bar();
}
int main()
{
foo_it(Foo());
}

How can I build a function concept with a particular signature?

I'm writing some generic software using concepts and I want to check if a particular function symbol name exists with a signature of (void)(int,int) on a struct. To do this I've I'm thinking of approaching this problem through template specialization but I'm sort of lost.
What I want is something to work and through compile time errors if the concept is not satisfied like so:
struct TypeA {
// Passes concept
void process (int a ,int b) const {};
};
struct TypeB {
// Does not pass concept
void process (float a) const {};
};
struct TestConcepts {
/* concept code here */
TestConcepts(T t) {
process_concept(t.process);
};
};
int main(void) {
// Should pass
TestConcept(TypeA{});
// Should throw error
TestConcept(TypeB{});
return 0;
}
I'm having a hard time filling in the blanks but this is what I have so far:
struct TestConcepts {
/* concept code here */
struct process_concept {
process_concept((V*)(IA,IB)){
if (is_integral<IA>::value && is_integral<IB>::value && is_same<V, void>) {
return;
}
static_assert(false, "You must provide function called process of type (void)(int,int)");
};
};
TestConcepts(T t) {
process_concept(&t.process);
};
};
Unfortunately this doesn't work. How can I get this function signature correct?
How about using a function that returns a declared function pointer?
struct TypeA {
// Passes concept
void process (int a ,int b) const {};
};
struct TypeB {
// Does not pass concept
void process (float a) const {};
};
template<typename T>
auto TestConcepts(T) -> void(T::*)(int, int) const
{
return &T::process;
}
int main(void) {
// Should pass
TestConcepts(TypeA{});
// Should throw error
TestConcepts(TypeB{});
return 0;
}
Output:
Error(s):
source_file.cpp: In instantiation of ‘void (T::* TestConcepts(T))(int, int) const [with T = TypeB]’:
source_file.cpp:26:23: required from here
source_file.cpp:19:16: error: cannot convert ‘void (TypeB::*)(float) const’ to ‘void (TypeB::*)(int, int) const’ in return
return &T::process;
^
EDIT: more options
If you want to include void process(long int a, long int b) const; or void process(int a, int b, int c=0) const;, like aschepler is suggesting, you can use type traits.
struct TypeA {
// Passes concept
void process(int a, int b) const {};
};
struct TypeB {
// Does not pass concept
void process(float a) const {};
};
struct TypeC {
// Passes concept
void process(long int a, long int b) const {};
};
struct TypeD {
// Passes concept
void process(int a, int b, int c = 0) const {};
};
struct TypeE {
// Does not pass concept
void process(int a, int b, int c) const {};
};
#include <type_traits>
template<typename T, typename A1, typename A2, typename... An>
typename std::enable_if<
std::is_integral<A1>::value &&
std::is_integral<A2>::value
>::type
TestProcess(const T& t, void(T::*)(A1, A2, An...) const) {
t.process(1, 2);
};
template<typename T>
void TestConcepts(const T& t)
{
TestProcess(t, &T::process);
}
int main(void) {
// Passes
TestConcepts(TypeA{});
// Throws compilation error
TestConcepts(TypeB{});
// Passes
TestConcepts(TypeC{});
// Passes
TestConcepts(TypeD{});
// Throws compilation error
TestConcepts(TypeE{});
return 0;
}

Explicit instantiation for concept checking

We have:
template<typename T>
struct A {
void foo(int a) {
T::foo(a);
}
};
template<typename T>
struct B {
template struct A<T>; // concept check
};
So, I define a concept checker A that checks T by forwarding foo to T::foo.
Now, I want to check whether the argument passed to B satisfies the concept A by explicit instantiation, but the compiler complains that it's the wrong namespace. How can I fix that?
Something like this perhaps:
template<typename T, void(T::*)(int)>
struct A {};
template<typename T>
struct B {
using Check = A<T, &T::foo>;
};
Demo
Or this:
template<typename T>
struct B {
static_assert(
std::is_same<decltype(&T::foo), void(T::*)(int)>::value,
"No T::foo(int) member");
};
So, I found a working example:
#include <tuple>
template<typename A>
struct IA : A {
void foo(int a) {
A::foo(a);
}
void bar(double a) {
A::bar(a);
}
static constexpr auto $ = std::make_tuple(&IA::foo, &IA::bar);
};
template<typename T>
struct B {
// trigger concept/interface check of T "implements" IA
static constexpr auto $ = IA<T>::$;
};
struct Test {
void foo(int a) {}
void bar(int a, int b) {}
};
int main() {
B<Test> b;
b = b;
}
The generation of $ in the structs triggers the compilation. The compiler in the example above correctly complains with:
In instantiation of 'void IA<A>::bar(double) [with A = Test]':
13:57: required from 'constexpr const std::tuple<void (IA<Test>::*)(int), void (IA<Test>::*)(double)> IA<Test>::$'
18:27: recursively required from 'constexpr const std::tuple<void (IA<Test>::*)(int), void (IA<Test>::*)(double)> B<Test>::$'
18:27: required from 'struct B<Test>'
28:13: required from here
10:17: error: no matching function for call to 'IA<Test>::bar(double&)'
10:17: note: candidate is:
24:10: note: void Test::bar(int, int)
24:10: note: candidate expects 2 arguments, 1 provided

C++ template and ambiguous function call

Some code I have no control over has a number of overloaded functions which accepts different types
i.e.
setValue(int)
setValue(std::string)
setValue(bool)
And I have a template function which would idealy take any one of these types and pass it on to the correct setValue function.
template <class T>
do_something(T value) {
...
setValue(value);
But I get this error
error: call to member function 'SetValue' is ambiguous
Is there anything I can do to work around this problem without copy and pasting my code for each type like the writers of setValue have?
by defining you own SetValue with exact match and forwarding to the correct overload.
void setValue(int i) { setValue(static_cast<double>(i)) }
or (if you have a lot of "setValue" functions with same type) you may help the compiler to choose which overload to use like this:
void setValue(char a);
void setValue(double a);
template <typename T>
struct TypeToUseFor
{
typedef T type;
};
template <>
struct TypeToUseFor<int>
{
typedef double type;
};
template <class T>
void func(T value)
{
setValue(static_cast<typename TypeToUseFor<T>::type>(value));
// setValue(value);
}
int main() {
func(0); // int -> ?
func('0'); // exact match
func(0.0); // exect match
func(0.f); // float -> double
return 0;
}
I have no problems with:
void setValue(int a)
{
}
void setValue(std::string a)
{
}
void setValue(bool a)
{
}
template <class T>
void func(T value)
{
setValue(value);
}
int main()
{
func(5);
}
Here is my run: http://codepad.org/1wq8qd7l

How to make a functor from a member function?

I want run to call c.drive():
#include <functional>
using namespace std;
struct Car {
void drive() { }
};
template <typename Function>
void run(Function f) {
f();
}
int main() {
Car c;
run(bind1st(mem_fun(&Car::drive), &c));
return 0;
}
This does not compile and the error messages does not help me:
at f():
no match for call to ‘(std::binder1st<std::mem_fun_t<void, Car> >) ()’
at the call to run:
no type named ‘first_argument_type’ in ‘class std::mem_fun_t<void, Car>’
no type named ‘second_argument_type’ in ‘class std::mem_fun_t<void, Car>’
No boost please.
Update: even though the problem is solved, I would be very happy to see TR1/C++0x solutions!
The Boost/TR1/C++0x solution is quite straightforward:
run(std::bind(&Car::drive, &c));
bind1st makes a unary function out of a binary function and a value. You are trying to make a function that takes no parameters out of a unary function and there isn't anything to support this in standard C++03.
You will have to do something like this.
template<class X, void (X::*p)()>
class MyFunctor
{
X& _x;
public:
MyFunctor(X& x) : _x( x ) {}
void operator()() const { (_x.*p)(); }
};
template <typename Function>
void run(Function f) {
f();
}
int main() {
Car c;
run(MyFunctor<Car, &Car::drive>(c));
return 0;
}
The C++0x solution using lambdas - http://www.ideone.com/jz5B1 :
struct Car {
void drive() { }
};
template <typename Function>
void run(Function f) {
f();
}
int main() {
Car c;
run( [&c](){ c.drive(); } );
return 0;
}