subject: use deduced function template return type for a type definition

Hi everyone,

I want to define the value and the value_type of a certain class template. I have the following portion of code,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <tuple>

template < typename unused = void >
struct TC
{
  template < typename T >
  static auto value(T const& t)
  {
    return std::make_tuple( t );
  }

  template < typename T >
  using value_type = decltype( value( T() ) );
};


this code require the type T to be default constructible. My question is, what about the case where T is not default constructible, how shall I proceed?

the full code is here:
http://coliru.stacked-crooked.com/a/280b863c52b86ee6

thank you for your attention.

Adim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <tuple>
#include <utility>

template < typename unused = void > struct TC
{
  template < typename T >  static auto value(T const& t) { return std::make_tuple( t ); }

  // http://en.cppreference.com/w/cpp/utility/declval
  template < typename T > using value_type = decltype( value( std::declval<T>() ) );
};

int main()
{
    struct A { A(int) {} } a(22) ;

    TC<>::value_type<A> tup = TC<>::value<A>(a) ;
    static_assert( std::is_same< std::tuple<A>, decltype(tup) >::value, "unexpected type" ) ;
}

http://coliru.stacked-crooked.com/a/9fd780c0a9c7ec49
Hi JLBorges,
this is what I have been looking for, thank you very much!
Topic archived. No new replies allowed.