After 1 or 2 hours spent to isolate a compilation error surrounded by a metaprogramming mess generating awfull compilation messages, here is a minimal and simple example that illustrates my problem:
#include <iostream>
#include <type_traits>
#include <array>
#include <utility>
#include <tuple>
template <class Crtp, class... Types>
struct Base
{
Base(const Types&... rhs) :
data(std::forward_as_tuple(rhs...)) {;}
std::tuple<Types...> data;
};
struct Derived
: public Base<Derived, std::array<double, 3>>
{
template <class... Args>
Derived(Args&&... args) :
Base<Derived, std::array<double, 3>>(std::forward<Args>(args)...) {;}
};
int main(int argc, char* argv[])
{
Derived a(std::array<double, 3>({{1, 2, 3}}));
Derived b(a);
Derived c(std::array<double, 3>());
Derived d(c); // Not working : why ?
return 0;
}
This is compiled with g++ 4.8.1 and I do not understand exactly why the compiler complain when I try to copy c
in d
and not a
in b
.
Here is the error:
main.cpp: In instantiation of ‘Derived::Derived(Args&& ...) [with Args = {Derived (&)(std::array<double, 3ul> (*)())}]’:
main.cpp:28:16: required from here
main.cpp:20:73: error: no matching function for call to ‘Base<Derived, std::array<double, 3ul> >::Base(Derived (&)(std::array<double, 3ul> (*)()))’
Base<Derived, std::array<double, 3>>(std::forward<Args>(args)...) {;}
^
main.cpp:20:73: note: candidates are:
main.cpp:10:5: note: Base<Crtp, Types>::Base(const Types& ...) [with Crtp = Derived; Types = {std::array<double, 3ul>}]
Base(const Types&... rhs) :
^
main.cpp:10:5: note: no known conversion for argument 1 from ‘Derived(std::array<double, 3ul> (*)())’ to ‘const std::array<double, 3ul>&’
main.cpp:8:8: note: constexpr Base<Derived, std::array<double, 3ul> >::Base(const Base<Derived, std::array<double, 3ul> >&)
struct Base
^
main.cpp:8:8: note: no known conversion for argument 1 from ‘Derived(std::array<double, 3ul> (*)())’ to ‘const Base<Derived, std::array<double, 3ul> >&’
main.cpp:8:8: note: constexpr Base<Derived, std::array<double, 3ul> >::Base(Base<Derived, std::array<double, 3ul> >&&)
main.cpp:8:8: note: no known conversion for argument 1 from ‘Derived(std::array<double, 3ul> (*)())’ to ‘Base<Derived, std::array<double, 3ul> >&&’
This is the most vexing parse:
Derived c(std::array<double, 3>());
is a declaration of a function c
which returns a Derived
and takes one unnamed argument of type pointer to function that takes no argument and returns std::array<double, 3>
.
Therefore Derived d(c)
tries to call Derived
constructor from the function c
. This is what GCC is saying here:
main.cpp: In instantiation of ‘Derived::Derived(Args&& ...) [with Args = {Derived (&)(std::array<double, 3ul> (*)())}]’:
Try this:
Derived c{std::array<double, 3>{}};