c++ - 无法复制从默认构造的数组构造的对象

标签 c++ templates c++11 metaprogramming stdarray

在花了 1 或 2 个小时来隔离编译错误,该编译错误被元编程困惑所包围,生成了可怕的编译消息,这里是一个最小的简单示例来说明我的问题:

#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;
}

这是用 g++ 4.8.1 编译的,我不明白为什么当我尝试在 d 而不是 a< 中复制 c 时编译器会报错b 中。

这里是错误:

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> >&&’

最佳答案

这是 most vexing parse :

Derived c(std::array<double, 3>());

是一个函数的声明c返回 Derived并接受一个类型为 的未命名参数指向函数,该函数不接受任何参数并返回 std::array<double, 3> 。 因此Derived d(c)尝试调用 Derived来自函数的构造函数 c .这就是 GCC 在这里所说的:

main.cpp: In instantiation of ‘Derived::Derived(Args&& ...) [with Args = {Derived (&)(std::array<double, 3ul> (*)())}]’:

试试这个:

Derived c{std::array<double, 3>{}};

关于c++ - 无法复制从默认构造的数组构造的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17486767/

相关文章:

c++ - Arduino:无法将取消引用的指针值从库函数返回到草图

c++ - 线程意外结束。 C++

c++ - 循环时字符的顺序是什么

PHP Smarty - 获取模板中所有变量的列表?

c++ - 这是 C++11 for 循环的已知缺陷吗?

c++ - Flex token 不适用于 char* 哈希表

c++ - 用于在C++中表示JSON的数据类型

c++ - 在 Qtcp readyRead() 中读取可变长度消息

c++ - 使用外部模板时出现链接器错误

c++ - 从 C++ 中的模板函数返回字符串和整数