我不明白为什么在foobar下面我需要指定std::vector<int>{}而在foobar2我不需要:
#include <iostream>
#include <memory>
#include <vector>
#include <tuple>
std::tuple<std::unique_ptr<int>, std::vector<int>> foobar() {
std::unique_ptr<int> test = std::make_unique<int>(42);
return { std::move(test), {} }; // <= this is a syntax error
// return { std::move(test), std::vector<int>{} } // <= this compiles...
}
std::tuple<int, std::vector<int>> foobar2() {
return { {}, {} };
}
int main() {
std::cout << *std::get<0>(foobar()) << "\n";
std::cout << std::get<0>(foobar2()) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
来自 GCC 的错误消息是
<source>: In function 'std::tuple<std::unique_ptr<int, std::default_delete<int> …Run Code Online (Sandbox Code Playgroud) std::get 似乎不是SFINAE友好的,如下面的测试用例所示:
template <class T, class C>
auto foo(C &c) -> decltype(std::get<T>(c)) {
return std::get<T>(c);
}
template <class>
void foo(...) { }
int main() {
std::tuple<int> tuple{42};
foo<int>(tuple); // Works fine
foo<double>(tuple); // Crashes and burns
}
Run Code Online (Sandbox Code Playgroud)
目标是将第二次呼叫foo转向第二次超载.在实践中,libstdc ++给出:
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.3.0/../../../../include/c++/6.3.0/tuple:1290:14: fatal error: no matching function for call to '__get_helper2'
{ return std::__get_helper2<_Tp>(__t); }
^~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
libc ++更直接,直接static_assert引爆:
/usr/include/c++/v1/tuple:801:5: fatal error: static_assert failed "type not found in type list"
static_assert ( value != -1, "type not found …Run Code Online (Sandbox Code Playgroud) 的实施方式std::tuple在两个的libstdc ++和的libc ++使用(I假定)的空基类优化压缩空元素:
struct empty {};
static_assert(sizeof(std::tuple<int, empty>) == sizeof(int), ""); // (1)
Run Code Online (Sandbox Code Playgroud)
我的问题很简单,标准是否规定了这种行为?也就是说,我可以依赖(1)始终如一地符合标准的实施吗?
这是 MWE:
#include <iostream>
#include <tuple>
#include <queue>
using namespace std;
bool operator<(tuple<int, int, int> lhs, tuple<int, int, int> rhs)
{
return get<1>(lhs) < get<1>(rhs);
}
int main()
{
priority_queue<tuple<int, int, int>> q;
q.push(make_tuple(2, 5, 3));
q.push(make_tuple(2, 3, 3));
cout << get<1>(q.top());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,无论我输入<还是>在句子中return get<1>(lhs) < get<1>(rhs);,输出总是5. 为什么会出现这种情况?
C++ 11引入了一个名为的对象std::ignore:
const /* unspecified */ ignore;
Run Code Online (Sandbox Code Playgroud)
为简洁起见,让我们
typedef decltype(std::ignore) T;
Run Code Online (Sandbox Code Playgroud)
据我所知,由于[C++ 11,20.4.2.4:7] 的规范,唯一的要求T就是它.CopyAssignablestd::tie
在g ++ - 4.8中,我发现T另外DefaultConstructible(例如,T x;编译).这是实现定义的行为吗?
(如果T我有其他要求,请详细说明.)
我想使用的元组组成的int,char,char在我的unordered_map.我这样做:
#include <string>
#include <unordered_map>
#include <cstring>
#include <iostream>
#include <tuple>
using namespace std;
tuple <int,char,char> kk;
unordered_map<kk,int> map;
int main()
{
map[1,"c","b"]=23;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这给了我以下错误:
map.cpp:9:21: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc> class std::unordered_map’
map.cpp:9:21: error: expected a type, got ‘kk’
map.cpp:9:21: error: template argument 3 is invalid
map.cpp:9:21: error: template argument 4 is invalid …Run Code Online (Sandbox Code Playgroud) 我正在看一些代码,我看到以下功能:
template <typename... Args>
static return_t make_return(Args &&... args)
{
// using std::forward<Args> will preserve lvalue args as such, but the point of this function
// is to make a return, where the 99.9+% case is moving a local (lvalue) into the return pack.
// Thus it forces a move, which will move `T&` args (but _not_ `const T&` args) into the
// return pack so that users don't need to type out a bunch of std::moves themselves/
// If …Run Code Online (Sandbox Code Playgroud) 我有一个函数at旨在通过运行时指定的索引访问std :: tuple元素
template<std::size_t _Index = 0, typename _Tuple, typename _Function>
inline typename std::enable_if<_Index == std::tuple_size<_Tuple>::value, void>::type
for_each(_Tuple &, _Function)
{}
template<std::size_t _Index = 0, typename _Tuple, typename _Function>
inline typename std::enable_if < _Index < std::tuple_size<_Tuple>::value, void>::type
for_each(_Tuple &t, _Function f)
{
f(std::get<_Index>(t));
for_each<_Index + 1, _Tuple, _Function>(t, f);
}
namespace detail { namespace at {
template < typename _Function >
struct helper
{
inline helper(size_t index_, _Function f_) : index(index_), f(f_), count(0) {}
template < typename _Arg …Run Code Online (Sandbox Code Playgroud) 我无法std::tuple从std::tuple兼容类型中逐个元素地初始化元素.为什么它不起作用boost::tuple?
#include <tuple>
#include <boost/tuple/tuple.hpp>
template <typename T>
struct Foo
{
// error: cannot convert 'std::tuple<int>' to 'int' in initialization
template <typename U>
Foo(U &&u) : val(std::forward<U>(u)) {}
T val;
};
int main()
{
boost::tuple<Foo<int>>{boost::tuple<int>{}}; // ok
auto a = boost::tuple<int>{};
boost::tuple<Foo<int>>{a}; // ok
std::tuple<Foo<int>>{std::tuple<int>{}}; // fails with rvalue
auto b = std::tuple<int>{};
std::tuple<Foo<int>>{b}; // fails with lvalue
}
Run Code Online (Sandbox Code Playgroud)
住在Coliru(GCC或Clang和libstdc ++不编译,但 Clang和libc ++编译没有错误)
std::tuple不做元素构造而是实例化Foo<int>::Foo<std::tuple<int>>而不是Foo<int>::Foo<int>.我认为std::tuple::tuple超载没有.4和5完全是为了这个目的: …
我想实现一个带有仿tuple_map函数的泛型函数,并将仿函数std::tuple应用于此元组的每个元素并返回std::tuple结果.实现非常简单,但问题出现了:这个函数应返回什么类型?我的实现使用了std::make_tuple.然而,在这里 std::forward_as_tuple建议.
更具体地说,实现(为简洁起见,省略了对空元组的处理):
#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>
template<class Fn, class Tuple, std::size_t... indices>
constexpr auto tuple_map_v(Fn fn, Tuple&& tuple, std::index_sequence<indices...>)
{
return std::make_tuple(fn(std::get<indices>(std::forward<Tuple>(tuple)))...);
// ^^^
}
template<class Fn, class Tuple, std::size_t... indices>
constexpr auto tuple_map_r(Fn fn, Tuple&& tuple, std::index_sequence<indices...>)
{
return std::forward_as_tuple(fn(std::get<indices>(std::forward<Tuple>(tuple)))...);
// ^^^
}
template<class Tuple, class Fn>
constexpr auto tuple_map_v(Fn fn, Tuple&& tuple)
{
return tuple_map_v(fn, std::forward<Tuple>(tuple),
std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
}
template<class Tuple, class Fn>
constexpr …Run Code Online (Sandbox Code Playgroud)