stringstream временный ostream возвращают проблему

Вы можете, но вам нужно будет разделить анимацию на полукруглые повороты. Я привожу пример этого в ответ на на этот вопрос , используя повторяющуюся CABasicAnimation, примененную к слою под видом. Как я предлагаю, выполнение этих половинных поворотов в качестве части CAKeyframeAnimation, вероятно, было бы лучшим способом структурировать это, потому что анимация была бы более плавной (в моем примере между половинными поворотами была небольшая задержка), и вы могли бы сделать хорошее ускорение / замедление в начале и в конце.

6
задан Marius 10 October 2009 в 07:48
поделиться

2 ответа

I think I see what's happening. This produces the expected output:

log(std::stringstream() << 1 << "hello");

while this does not:

log(std::stringstream() << "hello" << 1);

(it writes a hex number, followed by the "1" digit)

A few elements for the explanation:

  • An rvalue cannot be bound to a non-const reference
  • It is OK to invoke member functions on a temporary
  • std::ostream has a member operator<<(void*)
  • std::ostream has a member operator<<(int)
  • For char* the operator is not a member, it is operator<<(std::ostream&, const char*)

In the code above, std::stringstream() creates a temporary (an rvalue). Its lifetime is not problematic, as it must last for the whole full expression it is declared into (i.e, until the call to log() returns).

In the first example, everything works ok because the member operator<<(int) is first called, and then the reference returned can be passed to operator<<(ostream&, const char*)

In the second example, operator<<(cannot be called with "std::stringstream()" as a 1st argument, as this would require it to be bound to a non-const reference. However, the member operator<<(void*) is ok, as it is a member.

By the way: Why not define the log() function as:

void log(const std::ostream& os)
{
    std::cout << os.rdbuf() << std::endl;
}
7
ответ дан 8 December 2019 в 18:38
поделиться

Измените макрос LOG () на это:

#define LOG(x) do { std::stringstream s; s << x; log(s.str()); } while(0)

Это позволит вам использовать следующий синтаксис в журналах отладки, так что вам не придется вручную создавать поток строк.

LOG("Testing" << 1 << "two" << 3);

Затем определите его как ничего для выпуска, и у вас не будет дополнительных выделений.

6
ответ дан 8 December 2019 в 18:38
поделиться
Другие вопросы по тегам:

Похожие вопросы: