samedi 29 août 2015

Variadic template error -- MSVS2013 compiles, clang-3.5 does not

The code below compiles and runs fine with MSVC 2013, but not clang++3.6. Which compiler is correct?

MSVC 2013 compiles and executes the code, printing 26.04:

#include <iostream>

template <typename T, typename ... U>
auto mul(T t, U ... u) -> decltype(t * mul(u ...))
{
    return t * mul(u ...);
}

template <typename T>
T mul(T t) { return t; }

int main()
{
    std::cout << mul(2., 3.1, 4.2) << std::endl;
}

However, compiling with clang++-3.6 yields errors:

$ clang++ test.cpp -stdlib=libc++ -Wall -Wextra -std=c++14 
prog.cc:14:15: error: no matching function for call to 'mul'
        std::cout << mul(2., 3.1, 4.2) << std::endl;
                     ^~~
prog.cc:4:6: note: candidate template ignored: substitution failure [with T = double, U = <double, double>]: use of undeclared identifier 'mul'
auto mul(T t, U ... u) -> decltype(t * mul(u ...))
     ^                                 ~~~
prog.cc:10:3: note: candidate function template not viable: requires single argument 't', but 3 arguments were provided
T mul(T t) { return t; }
  ^
1 error generated.

Is the declaration of mul is not available to determine the return typedecl?

Aucun commentaire:

Enregistrer un commentaire