C ++显式返回类型模板专门化 [英] C++ explicit return type template specialisation

查看:205
本文介绍了C ++显式返回类型模板专门化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此(更一般的)问题的后续:上一个问题。此处提供了对当前问题的部分答案:部分回答本问题

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.

我对基于模板参数的返回类型的显式特殊化感兴趣。虽然上面的答案提供了一个问题的解决方案,我相信有一个更优雅的方式使用C + + 11/14技术解决问题:

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"));
}

上面的代码使用gcc 4.8.3编译和工作, std = c ++ 0x标志)。但是,它会发出以下警告:

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 函数使用 auto 类型说明符,不带结尾返回类型。

getOutputPort2 function uses auto type specifier without trailing return type.

根据我的理解,这将成为C ++ 14标准的一部分。但是,有没有一种方法在C ++ 11中实现上面的功能?此处可以使用 decltype

From my understanding this will become part of the C++14 standard. However, is there a way of implementing the functionality above in C++11? Can decltype be used here?

根据以下意见,我还想问一个另外的问题。从C ++ 14标准的角度来看,上面的代码是否有效?

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? If not, why not?

推荐答案

你可以扩展一个辅助模板类的想法,几乎把所有东西放在那里。它不是完全适合谁写的专业化,但它是非常方便的用户,谁可以调用 f< 0> f< 1> 等。实际上不需要 decltype ,但 decltype 确实使写入更容易。

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.

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!"; }
};

您可以沿着

#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!";
}

但我不相信这是一个改进只是写出 f_impl (更好的名称)。

but I'm not convinced it's an improvement over just writing out f_impl (with a better name) in full.

这篇关于C ++显式返回类型模板专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆