这是代码:
std::string Query_Set::get_sql() const throw()
{
std::stringstream out;
int counter = 0;
out << "WHERE " << time_name << " >= :start_time_" << counter++ << " AND " << time_name << " <= :finish_time_" << counter++ " AND something_else = :" << counter++;
return out.str();
}
这段代码很简单,但是当我打印出这个String时,它会做一些奇怪的事情:
"WHERE time >= :start_time_2 AND time <= :finish_time_1 AND something_else = :0"
我错过了一些简单的事吗?为什么计数器向后?
答案 0 :(得分:0)
在C ++中,对表达式的参数的评估顺序是未定义的。第二个counter++
可能在第一个之前发生。我甚至在Clang和GCC中看到过这种表达的不同行为。