简体   繁体   English

C ++变量生存期-需要变通方法以返回临时变量

[英]C++ variable lifetime — need workaround to return temporary

I have a C++ object ( boost::format ) that has a str() function which returns an std::string . 我有一个C ++对象( boost::format ),它具有一个str()函数,该函数返回std::string

So when I need a formatted C string, I need to write something like: 因此,当我需要格式化的C字符串时,我需要编写如下内容:

(boost::format("%1% %2%") % "1" % "2").str().c_str()

I find that rather verbose, and I need it a lot. 我觉得这很冗长,我非常需要它。 I thought of creating a derived class which has an operator char* and would work like this (Ch = char or wchar_t): 我想到创建一个派生类,该派生类具有operator char*并且将像这样工作(Ch = char或wchar_t):

operator Ch const* () const
{
    return str().c_str();
}

But of course, the string returned by str() is deallocated when the function returns, and no valid C string is returned. 但是,当然,当函数返回时,由str()返回的字符串将被释放,并且不会返回有效的C字符串。

Is there any kind of workaround? 有什么解决方法吗?

The workaround needs to create a string that exists as long as the surrounding function call: 解决方法需要创建一个字符串,只要存在周围的函数调用,该字符串就存在:

lib_function((boost::format("%1% %2%") % "1" % "2").str().c_str()); 
// can be deallocated here

The most obvious solution is to define a type which contains the std::string , and converts implicitly to a char const* . 最明显的解决方案是定义一个包含std::string的类型,并将其隐式转换为char const* Something like: 就像是:

class ToPlainC
{
    std::string myValue
public:
    ToPlainC( boost::format const& fmt )
        : myValue( fmt.str() )
    {
    }
    operator char const*() const
    {
        return myValue.c_str();
    }
};

which could be used: 可以使用:

lib_function( ToPlainC( boost::format( "%1% %2%" ) % "1" % "2" ) );

Such implicit conversions are not usually a good idea, but if you document the class well, that it should only be used for this particular scenario, I think it would be acceptable. 这种隐式转换通常不是一个好主意,但是如果您很好地记录了该类, 则只应将其用于此特定方案,我认为这是可以接受的。

EDIT: 编辑:

It occurs to me that to encourage only using this class as a temporary, in this particular scenario, you could name it using the naming conventions you normally use for functions, and not those you use for classes; 在我看来,为了鼓励只使用此类作为临时类,在这种特殊情况下,您可以使用通常用于函数的命名约定来命名它,而不是使用用于类的命名约定。 the user would then have the impression that he was using a function, and it would stick out like a sore thumb if he used it otherwise. 然后,用户会感到自己正在使用某个功能,如果不使用该功能,它会像拇指一样伸出来。

Return an std::string on the stack. 在堆栈上返回一个std :: string。 It's an extra copy, so it's a bad idea if performance is important, but it would work to eliminate most of the redundant typing. 这是一个额外的副本,因此如果性能很重要,则不是一个好主意,但是它可以消除大多数冗余类型。

You can define a structure like one shown below: 您可以定义如下所示的结构:

struct my_str
{
  const std::string &tmp;
  my_str(const boost::format &tmp) : tmp( tmp.str() ) {}
  operator const char *() const { return tmp.c_str(); }
};

And you can call it like 你可以这样称呼它

lib_function ( my_str(boost::format("%1% %2%") % "1" % "2") );

If you are worrying the reference would be dangling, read this article. 如果你担心,参考将被晃来晃去,请阅读文章。

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

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