Default template option for later

This link does not answer my question, so I will ask it here:

Basically, I want to write a template function

template <typename Out, typename In>
Out f(In x);

Here you always need to specify Outwhen calling f. I don't want to do this every time, so I basically want

template <typename Out = In, typename In>
Out f(In x);

This means that if I do not specify Out, the default will be In. However, this is not possible in C ++ 11.

So my question is: is there a way to achieve the effect:

  • the call f(t)will instantiate f<T,T>(t)or more generallyf<typename SomeThing<T>::type, T>
  • the call f<U>(t)will instantiatef<U, T>(t)
+5
source share
3 answers

! f<const int&> , , .

[hidden]$ cat a.cpp
#include <iostream>
#include <type_traits>
#include <typeinfo>
using namespace std;

template <typename Out, typename In>
Out f_impl(In x) {
  cout << "Out=" << typeid(Out).name() << " " << "In=" << typeid(In).name() << endl;
  return Out();
}

template <typename T, typename... Args>
struct FirstOf {
  typedef T type;
};

template <typename T, typename U>
struct SecondOf {
  typedef U type;
};

template <typename... Args, typename In>
typename enable_if<sizeof...(Args) <= 1, typename FirstOf<Args..., In>::type>::type f(In x) {
  typedef typename FirstOf<Args..., In>::type Out;
  return f_impl<Out, In>(x);
}

template <typename... Args, typename In>
typename enable_if<sizeof...(Args) == 2, typename FirstOf<Args...>::type>::type f(In x) {
  typedef typename FirstOf<Args...>::type Out;
  typedef typename SecondOf<Args...>::type RealIn;
  return f_impl<Out, RealIn>(x);
}

int main() {
  f(1);
  f(1.0);
  f<double>(1);
  f<int>(1.0);
  f<int>(1);
  f<const int>(1);
  f<int, double>(1);
  f<int, int>(1);
  f<double, double>(1);
}
[hidden]$ g++ -std=c++11 a.cpp
[hidden]$ ./a.out
Out=i In=i
Out=d In=d
Out=d In=i
Out=i In=d
Out=i In=i
Out=i In=i
Out=i In=d
Out=i In=i
Out=d In=d
+2

, , In, ?

:

template <typename Out, In>
Out f(In x);

template <typename T>
T f(T x);

:

f(42);
f<float>(42);

... , , f<int>(42). , SFINAE, :

template <
    typename Out,
    typename In,
    typename = typename std::enable_if<not std::is_same<Out, In>::value>::type
>
Out f(In x);

template <typename T>
T f(T x);

, , f_impl.

:

template <typename Out, typename In>
Out f_impl(In x) {
    std::cout << "f<" << typeid(Out).name() <<
                 ", " << typeid(In).name() <<
                 ">(" << x << ")\n";
    return x;
}

template <
    typename Out,
    typename In,
    typename = typename std::enable_if<not std::is_same<Out, In>::value>::type
>
Out f(In x) {
    std::cout << "f<Out, In>(x):\t ";
    return f_impl<Out, In>(x);
}

template <typename T>
T f(T x) {
    std::cout << "f<T>(x):\t ";
    return f_impl<T, T>(x);
}


int main() {
    f(42);
    f<float>(42);
    f<int>(42);
}
+8

You may not need this, but here is a classic technique:

struct Default
{
  template <typename Argument, typename Value>
    struct Get {
      typedef Argument type;
    };

  template <typename Value>
    struct Get <Default, Value> {
      typedef Value type;
    };
};

template <typename Out = Default, typename In>
typename Default::Get<Out, In>::type f(In x);
+4
source

All Articles