This is a follow up on this (more general) question: previous question. A partial answer to the present question is given here: partial answer to the present question.
I am interested in explicit specialisation of the return type based on the template argument. While the answer presented above provides a solution to the problem, I believe that there is a more elegant way of solving the problem using C++11/14 techniques:
template<int N> auto getOutputPort2();
template<> auto getOutputPort2<0>();
template<> auto getOutputPort2<1>();
template<>
auto getOutputPort2<0>()
{
return std::unique_ptr<int>(new int(10));
}
template<>
auto getOutputPort2<1>()
{
return std::unique_ptr<string>(new string("asdf"));
}
The code above compiles and works as expected using gcc 4.8.3 (with -std=c++0x flag). However, it issues the following warning: "'getOutputPort2' function uses 'auto' type specifier without trailing return type". From my understanding this will become part of the C++14 standard. However, is there a 'correct way' of implementing the functionality above in C++11? Can decltype be used here in some way?
Aucun commentaire:
Enregistrer un commentaire