Boost logo

Boost Users :

Subject: [Boost-users] function template argument deduction with std::tuple
From: Ireneusz Szcześniak (irek.szczesniak_at_[hidden])
Date: 2017-11-15 07:55:53


Hi,

I have a problem related to function template argument deduction with
std::tuple (with boost::tuple too). I attach a file with the complete
source you can compile. Below I describe the code.

I have a template function declaration:

> template <typename T>
> auto
> get_cost(const T &);

Then I define my template types:

> template <typename Graph>
> using Vertex = typename Graph::vertex_descriptor;
>
> template <typename Graph>
> using Label = std::tuple<Vertex<Graph>>;

I define the specialization of the get_cost function for the Label
type:

> template <typename Graph>
> auto
> get_cost(const Label<Graph> &l)
> {
> return std::get<0>(l);
> }

I can define alright a variable of type Label:

> struct graph
> {
> using vertex_descriptor = unsigned;
> };
>
> Label<graph> l;

But I cannot use the templated function:

> get_cost(l);

I get this error:

> error: use of 'auto get_cost(const T&) [with T =
> std::tuple<unsigned int>]' before deduction of 'auto'

I got this error with GCC HEAD 8.0.0 20171113 and Clang HEAD 6.0.0 (I
used wandbox.org).

Some other type definitions and their specializations of the get_cost
function compile. For a struct derived from the std::tuple:

> template <typename Graph>
> struct Label1: std::tuple<Vertex<Graph>>
> {
> };
>
> template <typename Graph>
> auto
> get_cost(const Label1<Graph> &l)
> {
> return std::get<0>(l);
> }

For a member of type std::tuple:

> template <typename Graph>
> struct Label2
> {
> std::tuple<Vertex<Graph>> t;
> };
>
> template <typename Graph>
> auto
> get_cost(const Label2<Graph> &l)
> {
> return std::get<0>(l.t);
> }

For a member of type Vertex<Graph>:

> template <typename Graph>
> struct Label3
> {
> Vertex<Graph> m_v;
> };
>
> template <typename Graph>
> auto
> get_cost(const Label3<Graph> &l)
> {
> return l.m_v;
> }

I would like to use the Label type, i.e., because it has the
constructor and operators already defined, which I need.

I would appreciate it if someone could explain what is going wrong
here.

Best,
Irek




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net