标签: stdtuple

为什么make_tie不是一个东西?

有一种流行的习惯std::tie用于实现比较运算符:

// foo.h
struct Foo {
  int a, b;
  string c;

  bool operator<(const Foo& rhs) const;
};

// foo.cc
bool Foo::operator<(const Foo& rhs) const {
  return tie(a, b, c) < tie(rhs.a, rhs.b, rhs.c);
}
Run Code Online (Sandbox Code Playgroud)

例如,它广泛用于Chromium

但是它需要复制成员列表,所以为什么不写一个辅助函数:

static auto MakeTie(const Foo& x) {
  return tie(x.a, x.b, x.c);
}
bool Foo::operator<(const Foo& rhs) const {
  return MakeTie(*this) < MakeTie(rhs);
}

// or, in foo.h
auto MakeTie() const;
// and in foo.cc
auto Foo::MakeTie() const { ... }
Run Code Online (Sandbox Code Playgroud)

(顺便说一下这样的成员函数不能从任何其他翻译单元调用)

那么,为什么我会看到数百个这样的tie(a, …

c++ stdtuple c++14

0
推荐指数
1
解决办法
99
查看次数

如何在编译时将同构元组转换为数组?

我想首先说明,当我说同构元组时,我的意思是每个元素的类型都是相同的,或者每个元素都有一个公共成员,该成员在所有元素中都是相同的类型。在这种情况下,公共成员也必须是static constexpr

这是描述我的问题的一段简化代码:

#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>

template <std::size_t N>
struct Int : std::integral_constant<std::size_t, N>
{
    Int() = default;
    Int(const Int&) = delete;
    Int(Int&&) = delete;

    Int& operator=(const Int&) = delete;
    Int& operator=(Int&&) = delete;
};

template <std::size_t... Ns>
struct Stuff
{
    using type = std::tuple<Int<Ns>...>;
    using arr_type = std::array<std::size_t, std::tuple_size_v<type>>;
    static constexpr arr_type arr = {Ns...};
};
Run Code Online (Sandbox Code Playgroud)

我相当确定它是正确的,唯一的问题是最后一行,但希望你明白要点。

现在,数组是使用Ns.... 我希望能够从而type不是构造数组(原因是type已经从元组中删除了重复类型(基于.value),并且还在.value此代码的实际版本中对类型进行了排序(基于))。如果相反,在编译时从数组中排序和删除重复值更容易,那么这也将被接受作为答案。

所以理想情况下,我会编写某种模板化结构来创建数组(因为在结构中会有一个static constexpr …

c++ compile-time variadic-templates stdtuple c++17

0
推荐指数
1
解决办法
159
查看次数

std::make_tuple() 不适用于 std::optional 类型的对象吗?

这是源代码:

using namespace std;

class obj {
    public:
        obj() = default;
        obj(int i) : i_{i} {}

        int         I()  const  {return i_;}
        int const & rI() const  {return i_;}
        void    I(int i)        {i_ = i;}
        void    show()  const   {cout << "addr = " << this << ", i_ = " << I() << endl;}

    private:
        int i_{0};
};

struct {
    optional<obj> o_0 {nullopt};
    optional<obj> o_1 {nullopt};
    optional<obj> o_2 {nullopt};

    void set_o0(obj o_) {o_0 = o_;}
    void set_o1(obj o_) {o_1 = o_;} …
Run Code Online (Sandbox Code Playgroud)

c++ stdtuple stdoptional

0
推荐指数
1
解决办法
422
查看次数

即使 T 是完整的,但 std::tuple_size&lt;T&gt; 类型不完整?

在下面的代码中,我尝试获取派生自 的自定义类型的元组大小std::tuple。但编译器抱怨这std::tuple_size是不完整的......我无法真正理解,因为struct foo此时已经明确定义了。自然也应该如此type_descriptor<foo>。这个错误是从哪里来的?

演示

#include <utility>
#include <tuple>
#include <cstdio>


struct foo
{
    int a_;
    int b_;
};

template <typename T>
struct type_descriptor
{};

template<>
struct type_descriptor<foo>
    : std::tuple<int, bool>
{
};

int main(){

    printf("Size of tuple = %zu\n", std::tuple_size_v<type_descriptor<foo>>);
}
Run Code Online (Sandbox Code Playgroud)

这会产生以下错误

<source>:89:27:   required from here
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/utility.h:75:61: error: incomplete type 'std::tuple_size<type_declarator<foo> >' used in nested name specifier
   75 |     inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
      |   
Run Code Online (Sandbox Code Playgroud)

c++ incomplete-type stdtuple c++20

0
推荐指数
1
解决办法
164
查看次数

完美转发失败的左值

我有一个实用函数,它迭代一个元组,并为每个元素调用一个函数与该元素,最后调用另一个函数与所有元组元素的结果.

为了更好地说明:

  • 有一个各种类型的元组 tuple<Foo<int>, Bar<double>, Baz<char>>
  • 每种类型Foo,BarBaz有一个隐式接口ClassT::data(),返回对某个内部成员的引用T&.
  • 你有一个签名功能 void (int, double, char)

该实用程序遍历元组成员,提取对内部成员的引用,并使用所需参数调用该函数.

问题:完美转发

我试图使用所谓的"通用引用"和完美转发来实现该实用程序,从而无需多个左值/右值和常量/非常量重载.

虽然函数的参数是类型的

template<typename Tuple>
auto invoke(Tuple&& tuple)
Run Code Online (Sandbox Code Playgroud)

我无法将左值绑定到它.为什么是这样?

我已经在这里提出了一个类似的问题,这个问题已经解决了; 但是,由于这是一个无关的问题,我认为这需要一个新的问题

关于ideone的示例:https://ideone.com/lO5JOB

#include <tuple>
#include <iostream>

// sequence

template<size_t...>
struct Sequence
{ };

template<size_t N, size_t... Seq>
struct GenerateSequence : GenerateSequence<N - 1, N - 1, Seq...>
{ };

template<size_t... Seq>
struct GenerateSequence<0, Seq...>
{
    using type = Sequence<Seq...>;
};

// invoke tuple …
Run Code Online (Sandbox Code Playgroud)

c++ perfect-forwarding c++11 stdtuple

-1
推荐指数
1
解决办法
204
查看次数

编译器错误?在 std::tuple 中返回 std::vector 和 std::string。但我得到了奇怪的价值观

我想返回多个值并使用 声明该函数auto

但这效果并不好。无法正确返回值。它被覆盖了。

我尝试执行以下功能f1f3。这些函数应该返回元组中的向量和字符串。但只是f3效果很好。

#include <iostream>
#include <vector>
#include <string>
#include <tuple>

auto f1(){
    std::vector<double> v(10, 0);
    std::string s = "hello";
    return std::forward_as_tuple(v, s);
}

auto f2(){
    std::vector<double> v(10, 0);
    return std::forward_as_tuple(v, "hello");
}

std::tuple<std::vector<double>, std::string> f3(){
    std::vector<double> v(10, 0);
    std::string s = "hello";
    return std::forward_as_tuple(v, s);   
}
int main(void){
    //change the function
    //auto [vec, str] = f1();
    //auto [vec, str] = f2();
    auto [vec, str] = f2();

    for (auto e : …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector auto stdtuple c++17

-1
推荐指数
1
解决办法
206
查看次数

std :: tuple是否接受auto作为类型

我必须将一个int和一个函数对象返回给调用者,我被认为是返回一个元组,就像make_tuple(int,[](some){})我现在在GCC上那样不支持decltpe(auto)作为返回类型,无论如何我std::tuple<int,auto> myfun()现在可以使我的返回类型我一直在做以下(但不确定)

auto myfun()->decltype(make_tuple(100,p))
{
   auto p=[](some){};
   return make_tuple(100,p);
}
Run Code Online (Sandbox Code Playgroud)

我在做什么好吗?

c++ auto c++11 stdtuple

-3
推荐指数
1
解决办法
732
查看次数

如何创建一个空的std :: tuple?

我需要构造一个std::tuple对象,以便std::tuple_size<T>::value=0。有没有办法做到这一点?

c++ stdtuple

-3
推荐指数
1
解决办法
124
查看次数