returning three values from a function - c++

Hi I wanted to implement tuple.
Let me know what is going wrong in this and how to implement it correctly. I want to return three values from an function where the first values will be Integers and the last value will be an array.
template <typename T1, typename T2, typename T3>
struct t_untuple
{
T1& a1;
T2& a2;
T3& a3;
explicit t_untuple(T1& a1, T2& a2, T3& a3) : a1(a1), a2(a2), a3(a3) { }
t_untuple<T1, T2, T3>& operator = (const tuple <T1, T2, T3>& p)
{
a1 = p.first;
a2 = p.second;
a3 = p.third;
return *this;
}
};
// Our functor helper (creates it)
template <typename T1, typename T2, typename T3>
t_untuple<T1, T2, T3> unpair(T1& a1, T2& a2, T3& a3)
{
return t_unpair<T1, T2, T3>(a1, a2, a3);
}
Help me out in this.
I am getting Symbol 'tuple' could not be resolved at const tuple & p due to which the p.third is also an error

If you know what types you want to return, why dont you use a simple struct:
template <typename T>
struct return_type {
int a;
int b;
std::array<T> c;
}

Assuming array type is invariant. Either use std::tuple
std::tuple<int,int,std::array<std::string>> MyFunc()
here is man page http://en.cppreference.com/w/cpp/utility/tuple
or create concrete class
struct IIA
{
int a;
int b;
std::array<std::string> arr;
}
IAA Myfunc(){}

Looks like you try to expand code from using unpair and std::pair:
t_untuple<T1, T2, T3>& operator = (const tuple <T1, T2, T3>& p)
{
a1 = p.first;
a2 = p.second;
a3 = p.third;
return *this;
}
};
but access fields of tuple are done different way:
t_untuple<T1, T2, T3>& operator = (const std::tuple <T1, T2, T3>& p)
{
a1 = std::get<0>(p);
a2 = std::get<1>(p);
a3 = std::get<2>(p);
return *this;
}
};
you also need to replace unpair to untuple in your copy-paste:
template <typename T1, typename T2, typename T3>
t_untuple<T1, T2, T3> untuple(T1& a1, T2& a2, T3& a3)
{
return t_untuple<T1, T2, T3>(a1, a2, a3);
}
do not forget to #include <tuple> and you have to make sure that you compiler is at least in c++11 mode

Related

A compile-time counter for distinct Type identifier

I am writing some template meta programming code. For some reasons, I want to make every object in my code has different type. The original code looks like this:
template<unsigned int index>
class Class1{
};
template<typename T1, typename T2, unsigned int index>
class Class2{
std::tuple<T1*, T2*> v;
public:
Class2(T1* t1, T2* t2): v(std::tuple<T1*, T2*>(t1, t2)) {}
};
template<unsigned int index>
auto makeClass1() {
return Class1<index>();
}
template<unsigned int index, typename T1, typename T2>
auto mul(T1& t1, T2& t2) {
return Class2<T1, T2, index>(&t1, &t2);
}
int main() {
auto t1 = makeClass1<0>(); // Type of TT1 is Class1<0>
auto t2 = makeClass1<1>(); // Type of TT2 is Class1<1>
auto m1 = mul<0>(t1, t2);
auto m2 = mul<1>(t1, t2); // Type of m2 is different from type of m1.
}
This code is work, but I wish my code is easy to use. So I want to ask is there any solution that can make the code look like this:
template<unsigned int index>
class Class1{
};
template<typename T1, typename T2, unsigned int index>
class Class2{
std::tuple<T1*, T2*> v;
public:
Class2(T1* t1, T2* t2): v(std::tuple<T1*, T2*>(t1, t2)) {}
};
template<unsigned int index = IncreaseCounter<?>::value>
auto makeClass1() {
return Class1<index>();
}
template<unsigned int index = IncreaseCounter<?>::value, typename T1, typename T2>
auto operator*(T1& t1, T2& t2) {
return Class2<T1, T2, index>(&t1, &t2);
}
int main() {
auto t1 = makeClass1(); // Type of TT1 is Class1<0>
auto t2 = makeClass1(); // Type of TT2 is Class1<1>
auto m1 = t1*t2
auto m2 = t1*t2; // Type of m2 is different from type of m1.
}
Note: I think I need a compile-time counter. But except the macro solution:__COUNTER__ and __LINE__ , I can't find any other compile-time solution. The macro solution is ineffective to my code.
Except the compile-time counter, any other solution is ok.
Thank you for reading my question. Due to my poor english expression ability,please bear with me for the wrong sentences.
In C++20, you might do:
template <typename = decltype([]{})>
class Class1{};
template<typename T1, typename T2, typename = decltype([]{})>
class Class2{
std::tuple<T1*, T2*> v;
public:
Class2(T1* t1, T2* t2): v(std::tuple<T1*, T2*>(t1, t2)) {}
};
template <typename T = decltype([]{})>
auto makeClass1() { return Class1<T>();}
template<typename T1, typename T2, typename T = decltype([]{})>
auto operator*(T1& t1, T2& t2) {
return Class2<T1, T2, T>(&t1, &t2);
}
int main() {
auto t1 = makeClass1();
auto t2 = makeClass1(); // Type of t2 is different from type of t1.
auto m1 = t1*t2;
auto m2 = t1*t2; // Type of m2 is different from type of m1.
static_assert(!std::is_same_v<decltype(t1), decltype(t2)>);
static_assert(!std::is_same_v<decltype(m1), decltype(m2)>);
}
Demo.
Let's regard the core of your question:
auto m1 = t1*t2;
auto m2 = t1*t2; // Type of m2 is different from type of m1.
You have exactly the same expression (t1*t2) but you wish this expression to produce two different types!
Overall your idea of counting objects in compile time is loose. What would you expect in this case?
for (int i = 0; i < 10; ++i) {
auto m = t1*t2;
}
Returning back to your code: you somehow need to introduce this compile-time index, like you do in mul<index>: either in mul or in the result type (explicit type instead of auto + appropriate conversion).

Function with two variadic parameters

I intend to implement a template function which would accept two functions and their parameters list and then created two wrappers. I have already implemented similar solution to accept two function and create wrapper using std::bind() but using hard-coded parameters. The solution is something like this:
template <typename T1, typename T2, typename T3, typename T4>
myWrapper (T2 func1, T4 func2) {
std::function<T1>ff = std::bind(func1, 1, placeholders::_1);
std::function<T3>ff = std::bind(func1, 110, 20, "something");
}
As you see the parameters to std::bind() are hard-coded in both cases which I would like to read them through myWrapper() function. Is there any way to read two variadic templates (list of variables of arbitrary length and type) and send them to two std::bind() calls I have in the above code?
Thanks,
Dan
Not sure to understand what you want but I suppose you need to use std::tuple (or something similar) to separate the two sequences of args.
The following is an full working example of what I mean, according what I've understand of what do you want.
#include <tuple>
#include <string>
#include <utility>
#include <iostream>
#include <functional>
template <typename T1, typename T2, typename T3, typename T4,
typename ... Ts1, typename ... Ts2,
std::size_t ... Is1, std::size_t ... Is2>
auto myWrapperH (T3 func1, T4 func2, std::tuple<Ts1...> const & tp1,
std::tuple<Ts2...> const & tp2,
std::index_sequence<Is1...> const &,
std::index_sequence<Is2...> const &)
{
T1 f1 = std::bind(func1, std::get<Is1>(tp1)...);
T2 f2 = std::bind(func2, std::get<Is2>(tp2)...);
return std::make_pair(f1, f2);
}
template <typename T1, typename T2, typename T3, typename T4,
typename ... Ts1, typename ... Ts2>
auto myWrapper (T3 func1, T4 func2, std::tuple<Ts1...> const & tp1,
std::tuple<Ts2...> const & tp2)
{ return myWrapperH<T1, T2>(func1, func2, tp1, tp2,
std::make_index_sequence<sizeof...(Ts1)>{},
std::make_index_sequence<sizeof...(Ts2)>{}); }
int foo (int a, int b)
{ return a+b; }
std::size_t bar (int a, int b, std::string const & str)
{ return str.size() + a + b; }
int main ()
{
using fType1 = std::function<int(int)>;
using fType2 = std::function<long()>;
auto mwr = myWrapper<fType1, fType2>(&foo, &bar,
std::make_tuple(1, std::placeholders::_1),
std::make_tuple(110, 20, std::string{"something"}));
std::cout << mwr.first(5) << std::endl; // print 6
std::cout << mwr.second() << std::endl; // print 139
}
Unfortunately is a C++14 code (auto return type; std::index_sequence and std::make_index_sequence) but should be easy to adapt in C++11.
-- EDIT --
As pointed by Banan (thanks!) there is no need to explicit the type of returned functions (T1, T2).
Using the auto return type the example can be simplified as follows
#include <tuple>
#include <string>
#include <utility>
#include <iostream>
#include <functional>
template <typename F1, typename F2, typename ... Ts1, typename ... Ts2,
std::size_t ... Is1, std::size_t ... Is2>
auto myWrapperH (F1 func1, F2 func2, std::tuple<Ts1...> const & tp1,
std::tuple<Ts2...> const & tp2,
std::index_sequence<Is1...> const &,
std::index_sequence<Is2...> const &)
{ return std::make_pair(std::bind(func1, std::get<Is1>(tp1)...),
std::bind(func2, std::get<Is2>(tp2)...)); }
template <typename F1, typename F2, typename ... Ts1, typename ... Ts2>
auto myWrapper (F1 func1, F2 func2, std::tuple<Ts1...> const & tp1,
std::tuple<Ts2...> const & tp2)
{ return myWrapperH(func1, func2, tp1, tp2,
std::make_index_sequence<sizeof...(Ts1)>{},
std::make_index_sequence<sizeof...(Ts2)>{}); }
int foo (int a, int b)
{ return a+b; }
std::size_t bar (int a, int b, std::string const & str)
{ return str.size() + a + b; }
int main ()
{
auto mwr = myWrapper(&foo, &bar,
std::make_tuple(1, std::placeholders::_1),
std::make_tuple(110, 20, std::string{"something"}));
std::cout << mwr.first(5) << std::endl; // print 6
std::cout << mwr.second() << std::endl; // print 139
}
Regarding your idea of a variadicStruct you may wish to have a look at std::tuple.
template<class... Ts, class... Us>
void do_stuff(const std::tuple<Ts...>&, const std::tuple<Us...>&) {
std::cout << "two variadic packs with " << sizeof...(Ts)
<< " and " << sizeof...(Us) << " elements." << std::endl;
}
To be called like this:
do_stuff(std::make_tuple(4.7, 'x', 1.0, 4, 8l), std::make_tuple("foo", 1));
You can use std::make_tuple and std::apply, for example:
template <typename R, typename Func1, typename Args1>
void wrapper (Func1 f1, Args1 a1)
{
std::function<R> wrapped1 = [f1, a1]{ return std::apply(f1, a1); };
}
// ...
auto deg_to_rad = [](int x){ return x / 180.f * 3.1415f; };
wrapper<float>(deg_to_rad, std::make_tuple(45));
std::apply requires C++17 support. However, the linked page on cppreference contains a possible implementation that works in C++11.

Unknown return type in template

I have a function
template <typename T1, typename T2>
/*return type*/ foo(MyClass<T1>& bar1, MyClass<T2>& bar2)
{
if (something)
return bar1;
else
return bar2;
}
The problem is, that I don't know what will this function return: it can be either MyClass<T1> or MyClass<T2>.
How can I get it to work?
T1 and T2 are structs of 3 ints, known at compile time. The return type depends on the smaller of those 2: for example for T1 = <5, 1, 1> T2 = <4, 4, 7> return type should be MyClass<T2>.
Example usage:
MyClass<5, 2, 8> m1;
MyClass<4, 2, 1> m2;
cout << foo(m1, m2); //should print m2 (I have a method used for that)
What you are trying to achieve there cannot be done as is. The language does not allow you to have a function that, based on some runtime data, changes its return type. Depending on the problem that you are trying to solve, there might be different alternatives, like determining a common type that can be used to represent either bar1 or bar2 or reorganizing the code so that you don't need to return at all.
That is if something cannot be determined at compile time... if it can be determined you can have some metaprogramming in place to determine the return type to be the one you need.
You should provide a higher level description of the real problem, after which you might get better ideas as of what direction to take.
You can try something in the lines of:
template <bool value> struct Bool {};
template <typename T, typename U>
T f_impl(T t, U u, Bool<true>) { return t; }
template <typename T, typename U>
T f_impl(T t, U u, Bool<false>) { return u; }
template <int A, int B, int C, int D, int E, int F>
auto f(MyClass<A,B,C> a, MyClass<D,E,F> b)
-> f_impl(a, b, Bool<!(A < D
|| (A == D && B < E)
|| (A == D && B == E && C < F))>())
{
return f_impl(a, b, Bool<!(A < D
|| (A == D && B < E)
|| (A == D && B == E && C < F))>());
}
You can define two functions from which only one will be instantiated for given types:
template <typename T1, typename T2>
struct ChooseFirst;
template <int A1, int B1, int C1, int A2, int B2, int C2>
struct ChooseFirst<MyClass<A1, B1, C1>, MyClass<A2, B2, C2>> {
// this requires constexpr make_tuple (C++14)
static const bool value = std::make_tuple(A1, B1, C1) < std::make_tuple(A2, B2, C2);
// if in your implementation make_tuple is not constexpr, you can write the comparison manually:
// static const bool value = A1 < A2 || (A1 == A2 && (B1 < B2 || (B1 == B2 && C1 < C2)));
};
template <typename T1, typename T2>
typename std::enable_if<ChooseFirst<T1, T2>::value, T1&>::type foo(T1& bar1, T2&) {
return bar1;
}
template <typename T1, typename T2>
typename std::enable_if<!ChooseFirst<T1, T2>::value, T2&>::type foo(T1&, T2& bar2) {
return bar2;
}
Demo
I would suggest to make a callback instead of returning value, remember tell don't ask principle:
template<typename T>
struct op{
void operator ()(T t){}
};
template<>
struct op<int>{
void operator ()(int a){}
};
template<typename T>
struct Func : public op<T>{
int a;
};
template<typename T, typename T2>
void foo( Func<T> t, Func<T2> t2){
t.a = 3;
t2.a = 4;
if( t.a > t2.a ){
t( 3 );
}else{
t2( 5 );
}
}
Maybe there is a better solution. Or you can use the operator () in MyClass, just specialize it.
You could return a union type, that can either be T1 or T2.
Since the return type must be fixed as compile time, you must return something that can be either MyClass<T1> or MyClass<T2>. This could either be a generic object such as boost::any (a bit of an overkill for this situation) or a common base, a reference (or pointer) to which you then return. This requires your classes to be defined like
class MyClassBase { /* ... */ };
template<typename T>
class MyClass : MyClassBase { /* ... */ };
template<typename T1, typename T2>
MyClassBase& foo(MyClass<T1>&bar1,MyClass<T2>&bar2)
{
if (something)
return bar1;
else
return bar2;
}
You can actually use RTTI so that the base MyClassBase can tell what it actually is. In fact, this is roughly how boost::any works.
Of course, as David said, if something is known at compile time, then your code is not really a good design and you should instead use a different one, using compile-time solutions (via template meta-programming techniques).
Both bar1 and bar2 are references so they are both returned if you change them in some way in your function. You could use a return value to indicate which:
enum ReturnType
{
BAR1,
BAR2
}
ReturnType foo(MyClass<T1>& bar1, MyClass<T2>& bar2)
{
if (something)
return BAR1;
else
return BAR2;
}
int main()
{
MyClass<T1> t1;
MyClass<T2> t2;
ReturnType ret = foo(t1, t2);
if(ret == BAR1)
{
// do what you want in case of MyClass<T1>
}
else if(ret == BAR2)
{
// do what you want in case of MyClass<T2>
}
}
An other way that maybe is closer to what you want is to use a base class pointer:
class Base
{
}
template<typename T>
class MyClass : public Base
{
// ...
}
Base* foo(MyClass<T1>& bar1, MyClass<T2>& bar2)
{
if (something)
return &bar1;
else
return &bar2;
}
Just as mentioned by vsoftco in the comments and in Walters answer returning a reference works too if that is your preference.
Base& foo(MyClass<T1>& bar1, MyClass<T2>& bar2)
{
if (something)
return bar1;
else
return bar2;
}

General min and max - C++

Writing a general minimum function, Two questions came to my mind. The code works fine with any input type and different argument number:
namespace xyz
{
template <typename T1, typename T2>
auto min(const T1 &a, const T2 &b) -> decltype(a+b)
{
return a < b ? a : b;
}
template <typename T1, typename T2, typename ... Args>
auto min(const T1 &a, const T2 &b, Args ... args) -> decltype(a+b)
{
return min(min(a, b), args...);
}
}
int main()
{
cout << xyz::min(4, 5.8f, 3, 1.8, 3, 1.1, 9) << endl;
// ^ ^ ^
// | | |
// float double int
}
Is there a better replacement for decltype(a+b)? I thing there's a standard class which I can't remember, something like decltype(std::THE_RESULT<a,b>::type).
The returned type of that decltype(std::THE_RESULT<a,b>::type)is const & or not ?
std::common_type(c++11):
For non-specialized std::common_type, the rules for determining the
common type between every pair T1, T2 are exactly the rules for
determining the return type of the ternary conditional operator where
T1 and T2 are the types of its second and the third operands.
and
For arithmetic types, the common type may also be viewed as the type
of the (possibly mixed-mode) arithmetic expression such as T0() + T1()
+ ... + Tn().
Not sure about const&, but you could play with std::remove_cv and std::remove_reference (and std::is_reference to find out the answer).
In fact, here's a list of type support utilities. Knock yourself out.
After the answer and worth comments I did it as below:
template <typename T1, typename T2>
auto min(const T1 &a, const T2 &b)
-> typename std::common_type<const T1&, const T2&>::type
{
return a < b ? a : b;
}
template <typename T1, typename T2, typename ... Args>
auto min(const T1 &a, const T2 &b, const Args& ... args)
-> typename std::common_type<const T1&, const T2&, const Args& ...>::type
{
return min(min(a, b), args...);
}

Better way to say x == Foo::A || x == Foo::B || x == Foo::C || ...?

Let's say I have a bunch of well-known values, like this (but const char * is just an example, it could be more complicated):
const char *A = "A", *B = "B", *C = "C", *D = "D", *E = "E", *F = "F", *G = "G";
Now let's say I want to behave in a particular way if the result of some expression is in a subset of those:
if (some_complicated_expression_with_ugly_return_type == A ||
some_complicated_expression_with_ugly_return_type == C ||
some_complicated_expression_with_ugly_return_type == E ||
some_complicated_expression_with_ugly_return_type == G)
{
...
}
I find myself typing this sort of thing often enough that I would like a shorthand for it.
If the language was Python, I could easily say:
if some_complicated_expression_with_ugly_return_type in [A, C, E, G]:
...
Is there a well-known, portable way for me to express this similarly in C++03?
Note that the return type is itself ugly (almost as ugly as the return type of lambda expressions), so I certainly don't want to store it in a local variable.
But the return type does not have to match that of the constants -- for example, if the return type was std::string, it would not be implicitly convertible to const char *, but operator == would be perfectly fine for the comparison.
So far, the best solution I have is to say something like:
const char *items[] = { A, C, E, G };
if (std::find(items, items + sizeof(items) / sizeof(*items),
some_complicated_expression_with_ugly_return_type)
!= items + sizeof(items) / sizeof(*items))
{
...
}
but it's pretty darn ugly. Is there a better way, which also works for non-PODs?
If you have C++11:
auto res = some_complicated_expression_with_ugly_return_type;
if (res == A
|| res == C
|| res == E
|| res == G) {
}
if not, you can still eliminate the type declaration by using a template function:
template <class T>
bool matches(T t) {
return t == A || t == C || t == E || t == G;
}
if (matches(some_complicated_expression_with_ugly_return_type)) {
}
You could factor your current best solution into a template:
template<class A, class B, size_t n>
inline bool is_in(const A &a, B (&bs)[n]) {
return std::find(bs, bs + n, a) != bs + n;
}
which you can use like
X items[] = { A, C, E, G };
if (is_in(some_complicated_expression_with_ugly_return_type, items))
…
You could use a switch:
switch (some_complicated_expression_with_ugly_return_type) {
case A: case C: case E: case G:
// do something
default:
// no-op
}
This only works with integer and enum types, note.
For more complex types, you can use C++11's auto, or for C++03, boost's BOOST_AUTO:
auto tmp = some_complicated_expression_with_ugly_return_type;
// or
BOOST_AUTO(tmp, some_complicated_expression_with_ugly_return_type);
if (tmp == A || tmp == C || tmp == E || tmp == G) {
// ...
}
(Edit: Turns out my original trick with a dummy type didn't work, I was misled by a lucky accident in my tests. Let's try that again...)
With a couple of helper templates you can write a general solution for this kind of situation:
template <typename T1> class Matcher {
public:
explicit Matcher(T1 t1): val(t1), flag(false) {}
template <typename T2> Matcher& operator()(T2 t2)
{ flag |= val == t2; return *this; }
operator bool() const { return flag; }
private:
T1 val;
bool flag;
};
template <typename T1> Matcher<T1> match(T1 t1) { return Matcher<T1>(t1); }
// example...
string s = whatever;
if (match(s)("foo")("bar")("zap")) { do_something(); }
You can match against as many arguments as you want.
Expressions of the type
if (some_complicated_expression_with_ugly_return_type == A ||
some_complicated_expression_with_ugly_return_type == C ||
some_complicated_expression_with_ugly_return_type == E ||
some_complicated_expression_with_ugly_return_type == G)
{
...
}
are quite common in code (well, a pre-computed expression is anyway). I think the best you can do for readability is pre-compute the expression and keep it as is.
ugly_return_type x = some_complicated_expression_with_ugly_return_type;
if (x == A ||
x == C ||
x == E ||
x == G)
{
...
}
Developers are used to this type of syntax. This makes it a whole lot easier to understand when someone else is reading your code
It also expresses what you want perfectly. There's a reason this type of syntax is so widely used in existing code - because other alternatives are worse for readability.
Of course, you could wrap the condition in a function, but only if it's reusable and it logically makes sense (besides the point IMO).
This can be done using variadic functions in c++03 as follows:
template <typename T>
bool MatchesOne( T _lhs, int _count, ...)
{
va_list vl;
va_start(vl,_count);
for (int i=0;i<_count;i++)
{
int rhs=va_arg(vl,int);
cout << "rhs = " << rhs << endl;
if (_lhs == rhs) return true;
}
va_end(vl);
return false;
}
int main(){
float ff = 3.0;
if (MatchesOne(ff, 5, 1, 2, 4, 5, 3))
{
cout << "Matches" << endl;
}
return 0;
}
If you know the types of all the expressions will have the same type as _lhs, you can change int rhs=va_arg(vl,int); to T rhs=va_arg(vl,T);
You can also do this elegantly using variadic templates in c++11:
template<typename T, typename T2>
bool one_equal(const T & _v1, const T2 & _v2)
{
return _v1 == _v2;
}
template<typename T, typename T2, typename... Args>
bool one_equal(const T & _v1, const T2 & _v2, Args... args)
{
return _v1 == _v2 || one_equal(_v1, args...);
}
...
if (one_equal(some_complicated_expression, v1, v2, v3, v4))
{
}
Okay one final hack-ish solution. It works, but makes the implementer of this function do a lot of repetitive work.
template <typename T1, typename T2>
bool match_one(T1 _v1, T2 _v2)
{
return _v1 == _v2;
}
template <typename T1, typename T2, typename T3>
bool match_one(T1 _v1, T2 _v2, T3 _v3)
{
return _v1 == _v3 || match_one(_v1, _v2);
}
template <typename T1, typename T2, typename T3, typename T4>
bool match_one(T1 _v1, T2 _v2, T3 _v3, T4 _v4)
{
return _v1 == _v4 || match_one(_v1, _v2, _v3);
}
template <typename T1, typename T2, typename T3, typename T4, typename T5>
bool match_one(T1 _v1, T2 _v2, T3 _v3, T4 _v4, T5 _v5)
{
return _v1 == _v5 || match_one(_v1, _v2, _v3, _v4);
}
If not switch, maybe something like this, I didn't use it, but may be a draft to something working?
template <class ReturnType>
bool average(ReturnType expression, int count, ...)
{
va_list ap;
va_start(ap, count); //Requires the last fixed parameter (to get the address)
for(int j=0; j<count; j++)
if(expression==va_arg(ap, ReturnType))
return true;
return false
va_end(ap);
}
C++11:
template<typename T1, typename T2>
bool equalsOneOf (T1&& value, T2&& candidate) {
return std::forward<T1>(value) == std::forward<T2>(candidate);
}
template<typename T1, typename T2, typename ...T>
bool equalsOneOf (T1&& value, T2&& firstCandidate, T&&...otherCandidates) {
return (std::forward<T1>(value) == std::forward<T2>(firstCandidate))
|| equalsOneOf (std::forward<T1> (value), std::forward<T>(otherCandidates)...);
}
if (equalsOneOf (complexExpression, A, D, E)) { ... }
C++03:
template<typename T, typename C>
bool equalsOneOf (const T& value, const C& c) { return value == c; }
template<typename T, typename C1, typename C2>
bool equalsOneOf (const T& value, const C1& c1, const C2& c2) {
return (value == c2) || equalsOneOf (value, c1);
}
template<typename T, typename C1, typename C2, typename C3>
bool equalsOneOf (const T& value, const C1& c1, const C2& c2, const C3& c3) {
return (value == c3) || equalsOneOf (value, c1, c2);
}
template<typename T, typename C1, typename C2, typename C3, typename C4>
bool equalsOneOf (const T& value, const C1& c1, const C2& c2, const C3& c3, const C4& c4) {
return (value == c4) || equalsOneOf (value, c1, c2, c3);
}
template<typename T, typename C1, typename C2, typename C3, typename C4, typename C5>
bool equalsOneOf (const T& value, const C1& c1, const C2& c2, const C3& c3, const C4& c4, const C5& c5) {
return (value == c5) || equalsOneOf (value, c1, c2, c3, c4);
}
// and so on, as many as you need