gcc中的歧义请求-clang中的歧义

时间:2018-08-07 21:08:33

标签: c++ templates gcc lambda clang

我仍在学习lambdas / templates / overloading,并找到了Nick Anthanasiou的blog post。在那里,他介绍了如何遍历不同类型的元组的方法,每个元组的行为都不同。

我通过执行以下命令测试了代码的正常工作: clang ++ -std = c ++ 14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

但是我在尝试时遇到了问题 g ++ -std = c ++ 14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

我收到以下错误:

request for member 'operator()' is ambiguous
   (void)expander{0, ((void)f(std::get<Is>(std::forward<Tuple>(tuple))), 0)...};

如何解决歧义性,所以它对于g ++也可以运行?

#include <iostream>
#include <memory>
#include <algorithm>
#include <string>
#include <vector>
#include <type_traits>
#include <tuple>

using namespace std; 

template <class... F>
struct overload_set : F... 
{
  overload_set(F... f) : F(f)... {}  
};

template <class... F>
auto overload(F... f) 
{
  return overload_set<F...>(f...);   
}

template<class Func, class Tuple, size_t...Is>
void for_each_in_tuple(Func f, Tuple&& tuple, std::index_sequence<Is...>){
    using expander = int[];
    (void)expander { 0, ((void)f(std::get<Is>(std::forward<Tuple>(tuple))), 0)... };
}

template<class Func, class Tuple>
void for_each_in_tuple(Func f, Tuple&& tuple){
    for_each_in_tuple(f, std::forward<Tuple>(tuple),
               std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>());
}

int main()
{
    auto func = overload (
        [](int &val) { val *= 2; }, 
        [](string &arg) { arg += arg; }, 
        [](char &c) { c = 'x'; }
        );
    std::tuple<int, string, char> tup {1, "boom", 'a'};
    for_each_in_tuple(func, tup); 
    cout << get<0>(tup) << endl << get<1>(tup) << endl << get<2>(tup); 
}

0 个答案:

没有答案