mardi 27 octobre 2015

Variadic template using lambdas : error with g++ but running with clang++

While playing with variadic templates, classes, functions and lambdas, (from here) I found that following code is running with clang++ while not running with g++ :

#include <iostream>
#include <string>
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...);   
}
int main()
{
    auto func = overload (
        [](int &val) { val *= 2; }, 
        [](string &arg) { arg += arg; }, 
        [](char &c) { c = 'x'; }
        );
    int val = 10;
    string str = "stackoverflow";
    char ch = 's';
    cout << val << " : " << str << " : " << ch << endl;
    func(val);
    func(str);
    func(ch);
    cout << val << " : " << str << " : " << ch << endl;
    return 0;
}

For clang : coliru

For g++ : coliru

g++ is giving ambiguous operator() for func(val), func(str) and func(c). I think the operator() must not be ambiguous, as each one is having different arguments.

What's the problem with g++?

Aucun commentaire:

Enregistrer un commentaire