4

How to do this:

#include <tuple>

std::tuple<double, int[2]> f()
{
    return std::make_tuple(0., {1, 2});
}

int main()
{
    f();
    return 0;
}

could not convert 'std::make_tuple<{}>()' from 'std::tuple<>' to 'std::tuple'

Can't the compiler figure out what to do?

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Velkan
  • 7,067
  • 6
  • 43
  • 87

2 Answers2

5

If you necessary need to use a "compile-time vector", try array.

#include <array>
// ...

std::tuple<double, std::array<int, 2>> f() {
  return std::make_tuple(0., std::array<int, 2>{1, 2});
}

As an aggregate type, it can be initialized with aggregate-initialization given at most N initializers that are convertible to T.

BiagioF
  • 9,368
  • 2
  • 26
  • 50
2

I guess that is not possible. Read this Initializing std::tuple from initializer list

Instead, you may choose to pass a vector like this:

std::tuple<double, vector<int>> t = std::make_tuple(0.0, vector<int>{1,3});
Community
  • 1
  • 1
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
  • @Piotr Skotnicki, that "int pointer" example made me even verify with `sizeof` that `int[2]` doesn't decay to a ponter in the tuple. Anyway, for the practical application I'll go with `using MyTuple = std::tuple; ... return MyTuple(0., {1, 2});`. – Velkan Dec 12 '16 at 15:08