完美的传递 [英] Perfect pass-through

查看:95
本文介绍了完美的传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑一个与完美转发有一些相似性的问题,但是函数参数不会传递给被调用的函数,而是返回。这是为什么我称之为完美传递。



问题如下:



我们有一个函数,它通过引用(和可能一些额外的参数),修改该对象,并返回修改的对象。这样的函数的最着名的示例可以是针对iostream的运算符 operator>> p>

让我们使用iostreams作为例子,因为它允许很好地显示我的后。例如,有时候想做的一件事是:

  std :: string s =(std :: ostringstream )<<< foo<< bar<< baz).str(); 

当然不起作用,原因有两个:




  • std :: ostringstream()是一个右值,但运算符< 将一个左值作为第一个参数


  • ostream& (至少对于标准的实际上是 basic_ostream< CharT,Traits>& 从第一个参数推导出c $ c> CharT Traits




所以让我们假设我们要设计插入操作符,以便上面的工作(你显然不能为现有的操作符,但你可以为你自己的类) 。显然,解决方案应该具有以下特征:




  • 第一个参数可以接受左值或右值。


  • 返回类型应该与传入的类型相同。但是,它仍然只接受ostreams(即,派生自 basic_ostream




在此特定用例中,不需要添加第三个要求:




  • 如果第一个参数是一个右值,则返回的值也是如此,否则返回一个左值。 li>


这个额外的规则是这样的,你可以从通过函数传递的右值移动构造(我不知道如果C ++ 11流是可移动的,但它是一个更一般的方案,流是一个方便的例子)。



很明显,在C ++ 03中,这些要求不能全部得到满足。然而,在C ++ 11中,我们有右值引用,这应该使这成为可能。



这是我的尝试:

  #include< iostream> 
#include< sstream>
#include< string>

template< typename Ostream> struct is_ostream
{
typedef typename std :: remove_reference< Ostream> :: type candidate;
typedef typename candidate :: char_type char_type;
typedef typename candidate :: traits_type traits_type;
typedef std :: basic_ostream< char_type,traits_type> basic_ostream;
static const bool value = std :: is_base_of< basic_ostream,candidate> :: value;
};

class SomeType {};

template< typename Ostream>
typename std :: enable_if< is_ostream< Ostream> :: value,Ostream&> :: type
operator<<(Ostream&& is,SomeType const& x)
{
是<< SomeType;
return std :: forward< Ostream>(is);
}

int main()
{
SomeType t;

std :: string s =(std :: ostringstream()<< t).str();

std :: cout<< s < std :: endl;
}

它确实使用gcc编译(使用选项 std = c ++ 0x ),并且当按预期运行输出 SomeType 时。



因此我的问题:


  1. 我写的输出操作符确实按预期工作(并满足我提供的所有要求)?或者它可能以意想不到的方式失败或有其他意外的后果?特别是:我在这里使用 std :: forward 吗?


  2. 满足要求?


  3. 假设这是真的正确和最简单的方法:你认为这是一个好主意,



c>是一个右值,但是 cd>


有一个通用的插入器rvalue streams,但它会返回 basic_ostream< charT,traits>&

  template< class charT,class traits,class T> 
basic_ostream< charT,traits>&
operator<<(basic_ostream< charT,traits>&&&os,const T& x);

要为您的示例正常工作,必须返回流的派生类型$ c> std :: ostringstram )。



  1. 我写了它确实按预期工作(并满足我提供的所有要求)?或者它可能失败在
    意想不到的方式或有其他意想不到的后果?特别是:
    我使用 std :: forward 在这里修改吗?


您的代码对我看起来正确。



  1. 更容易的方式来满足要求?


您的代码看起来类似于我写的代码,



  1. 假设这是真的,最简单的方法是:考虑这是一个好主意,或者你会反对它
    (专门用于流输出如我的示例,作为一个更多的
    一般方案通过对象通过函数)?


这句话对我来说很好。



libc ++ 中,我定义了像下面这样的右值ostream插入器(作为扩展)。我试图让它标准化,但它是晚了,委员会是可以理解的不是更多的thrashing的心情:

 模板< class _Stream,class _Tp> 
inline
typename enable_if
<
!is_lvalue_reference< _Stream> :: value&&&
is_base_of< ios_base,_Stream> :: value,
_Stream&&&
> :: type
operator<<(_ Stream&& __os,const _Tp& __x)
{
__os< __X;
return std :: move(__ os);与您的不同,此接受右值流。
}



<但是像你的一样,它返回具体的派生类型,因此适用于你的示例。


I'm thinking about a problem which has some similarity with perfect forwarding, but where the function argument is not passed to a called function, but returned. This is why I call it "perfect pass-through".

The problem is the following:

Say we have a function which takes an object by reference (and possibly some extra arguments), modifies that object, and returns the modified object. The best-known example of such functions are probably operator<< and operator>> for iostreams.

Let's use iostreams as example, because it allows to nicely show what I'm after. For example, one thing which one would sometimes like to do is:

std::string s = (std::ostringstream() << foo << bar << baz).str();

Of course that doesn't work, for two reasons:

  • std::ostringstream() is an rvalue, but operator<< takes an lvalue as first argument

  • operator<< returns an ostream& (well, at least for the standard ones actually a basic_ostream<CharT, Traits>& where CharT and Traits are deduced from the first argument).

So let's assume we want to design the insertion operator so that the above works (you obviously can't do that for the existing operators, but you can do that for your own classes). Obviously the solution should have the following traits:

  • The first argument can accept either an lvalue or an rvalue.

  • The return type should be the same type as passed in. But of course it should still only accept ostreams (i.e. classes derived from an instantiation of basic_ostream).

While in this specific use case it isn't needed, I want to add a third requirement:

  • If the first argument is an rvalue, so is the returned value, otherwise an lvalue is returned.

This extra rule is so that you can move-construct from an rvalue passed through the function (I don't know if C++11 streams are move constructible, but it is intended to be a more general scheme, with the stream just as handy example).

It is obvious that in C++03, those requirements could not all be met. However, in C++11, we have rvalue references, which should make this possible.

Here's my try on this:

#include <iostream>
#include <sstream>
#include <string>

template<typename Ostream> struct is_ostream
{
  typedef typename std::remove_reference<Ostream>::type candidate;
  typedef typename candidate::char_type char_type;
  typedef typename candidate::traits_type traits_type;
  typedef std::basic_ostream<char_type, traits_type> basic_ostream;
  static const bool value = std::is_base_of<basic_ostream, candidate>::value;
};

class SomeType {};

template<typename Ostream>
 typename std::enable_if<is_ostream<Ostream>::value, Ostream&&>::type
  operator<<(Ostream&& is, SomeType const& x)
{
  is << "SomeType";
  return std::forward<Ostream>(is);
}

int main()
{
  SomeType t;

  std::string s = (std::ostringstream() << t).str();

  std::cout << s << std::endl;
}

It indeed compiles with gcc (using the option -std=c++0x), and when run outputs SomeType as expected. However, it is a quite complicated machinery to get there.

Therefore my questions:

  1. Is the output operator as I wrote it indeed working as expected (and fulfilling all the requirements I gave)? Or could it fail in unexpected ways or have other unexpected consequences? Especially: Is my use of std::forward correct here?

  2. Is there an easier way to meet the requirements?

  3. Assuming this is indeed correct and the simplest way to do it: Do you consider it a good idea to do that, or would you advice against it (both specifically for stream output as in my example, and as a more general scheme for passing objects through functions)?

解决方案

std::ostringstream() is an rvalue, but operator<< takes an lvalue as first argument

There's a generic inserter to take rvalue streams, but it returns a basic_ostream<charT, traits>&:

template <class charT, class traits, class T>
  basic_ostream<charT, traits>&
  operator<<(basic_ostream<charT, traits>&& os, const T& x);

To work correctly for your example it would have to return the derived type of the stream (std::ostringstram).

  1. Is the output operator as I wrote it indeed working as expected (and fulfilling all the requirements I gave)? Or could it fail in unexpected ways or have other unexpected consequences? Especially: Is my use of std::forward correct here?

Your code looks correct to me.

  1. Is there an easier way to meet the requirements?

Your code looks similar to the code I wrote to solve this problem (see below).

  1. Assuming this is indeed correct and the simplest way to do it: Do you consider it a good idea to do that, or would you advice against it (both specifically for stream output as in my example, and as a more general scheme for passing objects through functions)?

The idiom looks fine to me.

In libc++ I defined the rvalue ostream inserter like the following (as an extension). I made an attempt to get it standardized but it was late and the committee was understandably not in the mood for more thrashing:

template <class _Stream, class _Tp>
inline
typename enable_if
<
    !is_lvalue_reference<_Stream>::value &&
    is_base_of<ios_base, _Stream>::value,
    _Stream&&
>::type
operator<<(_Stream&& __os, const _Tp& __x)
{
    __os << __x;
    return std::move(__os);
}

Unlike yours, this only accepts rvalue streams. But like yours, it returns the concrete derived type, and so works with your example.

这篇关于完美的传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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