Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 生成具有make_tuple-array引用的生成问题_C++_C++11_Reference_Tuples_Pass By Reference - Fatal编程技术网

C++ 生成具有make_tuple-array引用的生成问题

C++ 生成具有make_tuple-array引用的生成问题,c++,c++11,reference,tuples,pass-by-reference,C++,C++11,Reference,Tuples,Pass By Reference,我有一个类,它将对大小为5的数组的引用作为构造函数中的参数之一 class sha1 { public: typedef unsigned int(&type)[5]; type digest_; sha1(const type digest) : digest_(digest) {} }; 我可以通过传递一个5的数组来实例化这个类。但是用调用std::make_tuple来代替它无法编译 int main(int argc, const char* c

我有一个类,它将对大小为5的数组的引用作为构造函数中的参数之一

class sha1 {
public:
    typedef unsigned int(&type)[5];
    type digest_;
    sha1(const type digest) : digest_(digest)
    {}
};
我可以通过传递一个5的数组来实例化这个类。但是用调用std::make_tuple来代替它无法编译

int main(int argc, const char* const argv[]) {
    unsigned int digest[5] = { 0 };
    const sha1 s(digest);
    const sha1 p = std::make_tuple(digest);   <-- error here
    return 0;
}
int main(int argc,const char*const argv[]{
无符号整数摘要[5]={0};
警司沙一s(摘要);;
const sha1 p=std::make_tuple(digest);将在构造
tuple
之前传递给它的参数,因此
无符号int(&)[5]
类型转换为
无符号int*
,这与
sha1
构造函数的参数类型不匹配

改为使用创建引用的元组

map.emplace(std::piecewise_construct,
            std::forward_as_tuple(digest, 10),
            std::forward_as_tuple("test"));

你试过
std::tie
而不是
std::make_tuple
?顺便说一句,你的类没有将
std::tuple
作为构造函数…你对
unordered_map
的声明是什么?我的意思是你有两个不同的问题。@stophen我添加了一个使用unordered_map的例子,这很有帮助。除了deay之外,还有什么不同的地方吗在make_tuple和forward_as_tuple之间?有什么理由使用make_tuple吗?afaik forward_as_tuple应该一直工作吗?@Shankaran正如我在回答中所说,
forward_as_tuple
创建一个引用元组,而
make_tuple
将复制/移动参数。所以你的问题有点像提问“为什么我要使用
T
,而
T&
不应该一直工作吗?”
#include <iostream>
#include <unordered_map>

class sha1 {
public:
    typedef unsigned int(&type)[5];
    const type digest_;
    const int index_;
    sha1(const type digest, int index) : digest_(digest), index_(index)
    {}
    bool operator==(const sha1& that) const {
        return true;
    }
};

namespace std {
    template<> struct hash<sha1> {
        inline size_t operator()(const sha1& p) const {
            return 0;
        }
    };
}

int main(int argc, const char* const argv[]) {
    unsigned int digest[5] = { 0 };

    const sha1 s(digest, 10); // works

    std::unordered_map<sha1, std::string> map;

    map.insert(std::make_pair(sha1(digest, 10), "test")); // works

    map.emplace(std::piecewise_construct, std::make_tuple(digest, 10), std::make_tuple("test"));  // <-- error here

    return 0;
}
map.emplace(std::piecewise_construct,
            std::forward_as_tuple(digest, 10),
            std::forward_as_tuple("test"));