简体   繁体   English

C ++显式返回类型模板专门化

[英]C++ explicit return type template specialisation

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: 尽管以上给出的答案提供了解决问题的方法,但我相信,使用C ++ 11/14技术可以解决该问题的方法更为优雅:

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). 上面的代码使用gcc 4.8.3(带有-std = c ++ 0x标志)进行编译并按预期工作。 However, it issues the following warning: 但是,它发出以下警告:

getOutputPort2 function uses auto type specifier without trailing return type. getOutputPort2函数使用auto类型说明符,而没有尾随返回类型。

From my understanding this will become part of the C++14 standard. 据我了解,这将成为C ++ 14标准的一部分。 However, is there a way of implementing the functionality above in C++11? 但是,有没有一种方法可以在C ++ 11中实现上述功能? Can decltype be used here? 可以在此处使用decltype吗?


EDIT. 编辑。 Following the comments below, I would also like to ask an additional question. 在下面的评论之后,我还要提出另一个问题。 Is the code above valid from the perspective of the C++14 standard? 从C ++ 14标准的角度来看,以上代码是否有效? If not, why not? 如果没有,为什么不呢?

You can extend the idea of a helper template class, and put pretty much everything in there. 您可以扩展帮助程序模板类的概念,并将几乎所有内容放在其中。 It's not exactly pretty for whoever has to write the specialisations, but it's very convenient for the user, who can just call f<0> , f<1> , etc. It doesn't really need decltype , but decltype does make it quite a bit easier to write. 对于必须编写专业知识的人来说,这不是很漂亮,但是对于用户来说非常方便,他们可以只调用f<0>f<1>等。它确实不需要 decltype ,但是decltype确实使它相当容易编写。

template <int N>
struct f_impl;

template <int N>
decltype(f_impl<N>::impl()) f()
{ return f_impl<N>::impl(); }

template <> struct f_impl<0> {
  static int impl() { return 1; }
};

template <> struct f_impl<1> {
  static const char *impl() { return " Hello, world!"; }
};

int main() {
  std::puts(f<1>() + f<0>());
}

You might be able to make it a bit more manageable with macros: instead of 您可以通过宏使它更易于管理:

template <> struct f_impl<1> {
  static const char *impl() { return " Hello, world!"; }
};

you could write something along the lines of 你可以写一些类似的东西

#define DEFINE_F(N, Result)      \
  template <> struct f_impl<N> { \
    static Result impl();        \
  };                             \
  Result f_impl<N>::impl()

DEFINE_F(1, const char *) {
  return " Hello, world!";
}

but I'm not convinced it's an improvement over just writing out f_impl (with a better name) in full. 但我不认为这是完全写出f_impl (具有更好的名称)的改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM