- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
An issue of streams, internal string, and operation ordering with MSVC versus GCC / Clang
obj.foo() << endl << obj.bar() << endl << [..etc]
)测试输出时,遇到了使用内部更新的字符串时既不期望也不期望的行为。在Linux上使用GCC或Clang遇到过。
#include <iostream>
#include <ostream>
#include <string>
class Sample {
std::string s;
int x;
public:
Sample() = default;
friend std::ostream& operator<<(std::ostream& os, const Sample& a);
// Update internal value, return the object.
Sample const& set(std::string ss, int xx) {
s = ss;
x = xx;
return *this;
}
// Update internal value, return the string.
std::string const& setStr(std::string ss, int xx) {
set(ss, xx);
return s;
}
// Update internal value, return the int.
int const& setX(std::string ss, int xx) {
set(ss, xx);
return x;
}
};
// Output the object integer, same behavior with the string
// or if we flush inside or anything.
std::ostream& operator<<(std::ostream& os, Sample const& a)
{
os << a.x;
return os;
}
int main() {
Sample a;
// GCC / Clang | MSVC
std::cerr << a.set("0", 0) << std::endl // 0 0
<< a.set("1", 1) << std::endl // 1 0
<< a.set("2", 2) << std::endl; // 2 0
std::cerr << "content : " << a << std::endl; // 2 0
a.set("",-1); std::cerr << std::endl;
std::cerr << a.setStr("0", 0) << std::endl // 0 0
<< a.setStr("1", 1) << std::endl // 1 0
<< a.setStr("2", 2) << std::endl; // 2 0
std::cerr << "content : " << a << std::endl; // 2 0
a.set("",-1); std::cerr << std::endl;
std::cerr << a.setX("0", 0) << std::endl // 0 0
<< a.setX("1", 1) << std::endl // 1 1
<< a.setX("2", 2) << std::endl; // 2 2
std::cerr << "content : " << a << std::endl; // 2 2
}
看起来,使用字符串或流版本,所有操作都使用相同的最终变异的字符串对象,但我不知道为什么这样做(再次,在GNU / Linux工具链上没有问题)。
std::cerr << a.set("0", 0) << std::endl; // "0"
std::cerr << a.set("1", 1) << std::endl; // "1"
std::cerr << a.set("2", 2) << std::endl; // "2"
我首先认为这是一个冲洗问题,但测试显示为其他情况。实际上,在每个链接的调用之间使用
endl
甚至
flush
都没有任何作用。
void println() // base function
{
std::cerr << std::endl;
}
template<typename T, typename... Ts>
constexpr void println(T head, Ts... tail)
{
std::cerr << head << std::endl;
println(tail...);
}
int main()
{
int i;
i = 0;
println(++i, ++i, ++i); // 3 3 3
i = 0;
println(i++, i++, i++); // 2 1 0
}
在MSVC上,流似乎像此后增量可变参数模板一样工作:结果以某种方式向后(或更像递归地应用后)。我不确定这对我是否有意义。
最佳答案
根据Microsoft C++ language conformance table,直到VS 2017 15.7才实现C++ 17的changed evaluation order rules。 14.0不够好。您将不得不升级或不升级。
测验
#include <iostream>
int f()
{
static int i = 0;
return i++;
}
int main()
{
std::cout << f() << f();
}
应该在C++ 17之后产生01
cerr
替换为
cout
以查看其中是否存在一些深奥的魔力),我得到了
#include <iostream>
#include <ostream>
#include <string>
class Sample {
std::string s;
int x = 0;
public:
Sample() = default;
friend std::ostream& operator<<(std::ostream& os, const Sample& a);
// Update internal value, return the object.
Sample const& set(std::string ss, int xx) {
std::cout << "in func with " << ss << std::endl;
s = ss;
x = xx;
return *this;
}
};
// Output the object integer, same behavior with the string
// or if we flush inside or anything.
std::ostream& operator<<(std::ostream& os, Sample const& a)
{
os << a.x;
return os;
}
int main() {
Sample a;
// GCC / Clang | MSVC
std::cout << a.set("0", 0) << std::endl // 0 0
<< a.set("1", 1) << std::endl // 1 0
<< a.set("2", 2) << std::endl; // 2 0
std::cout << "content : " << a << std::endl; // 2 0
}
和输出
- In a shift operator expression
E1<<E2
andE1>>E2
, every value computation and side-effect ofE1
is sequenced before every value computation and side effect ofE2
关于c++ - 链式ostream内部行为及其在MSVC上的结果(与Clang相比),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62666619/
一晃五年没写博客了,依旧再C#上耕耘,依旧没有啥建树,现在也不知道.net上还有多少人再使用,在这里分享一些自己觉得写的还算优雅的代码。 对于自己写着完的代码,我特别喜欢链式(来源于jQuer
我正在构建一个吉他和弦查找应用程序。我使用多维数组来表示指板。数组中的每个元素都由具有字符串属性“Note”的 FretSpace 结构表示。为了初始化指板上的音符属性,我传递了要处理的吉他弦的详细信
我在演示代码中使用 setTimeout 函数模拟了 3 个 ajax 调用。我将从一段运行良好的代码开始:所有调用都是并行进行的,我希望所有调用都能成功,否则会出现错误。 var p1 = func
谁能解释一下? a = [2,3,4] b = [5,6,8,9] print(len(a) > 0) print(len(b) > 0) print((len(a) > 0) & len(b) >
我正在处理具有多个子 JSONObject 的 JSONObject。这是我填写内容的方式: myJson.getJSONObject(CAT_NAME).put(VAR_NAME, var)
想象一下这种情况,我有一个需要检查属性的对象。但是,该对象当前可以具有空值。 如何在一个“if”条件下检查这两个条件? 目前,我必须做这样的事情: if (myObject != null) {
我有一个对象集合,称它们为obj。他们有一个 act() 方法。 act() 方法最终会导致 o 上的 event() observable 调用 onComplete。 链接这些的好方法是什么? 即
假设我有一个列表变量 datalist 存储 10,000 个字符串实体。QTableView 只需要显示其中的一些实体。这就是为什么 QTableView 被指定为 QSortFilterProxy
我正在寻找支持链式 MSI 安装的工具(最好不是 InstallShield,而且最好是便宜/免费的)。我有几个小型安装需要能够单独部署,但也需要作为一个组部署,我不想维护多个安装程序。 看起来我需要
在这种情况下,我想迭代集合中除最后 2 个元素之外的所有元素。 假设我采用了一种奇怪的方式,例如 x.Reverse().Skip(2).Reverse()。 每个 LINQ 操作是否会有效地生成一个
对于javascript来说非常陌生,我有两个html数字选择,包括年份,我想将第二个选择与第一个选择链接起来,这样当我在第一个选择中选择年份时(而第二个选择没有选项)首先),第二个选择应包括从所选数
有人可以向我解释一下为什么以下两个链式函数: // returns zero if okay var resetCounter = function (model) { return new Prom
所以我有 2 个 promise 函数。当第一个函数出现错误时,我希望它显示错误消息。当完成或失败时,我希望他们执行一个finally catch all 函数,但由于某种原因它不起作用。我的代码如下
我有一个函数 const func = () => server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyN
(async function() { var a,b; function flush(){ return new Promise(res => {
这个问题已经有答案了: Promise chaining: Use result from previous promise in next then callback [duplicate] (1
这可能不是专业正则表达式理解的问题。唯一重要的是因为我正在运行多个链式替换命令,这些命令会影响文本文件中的某些相同文本。我还想象在替换之前,根据分隔符词(需要多次替换)的使用方式对 txt 文件进行分
我正在尝试构建一组类来定义 OSI 堆栈中协议(protocol)的分层属性...从抽象意义上讲,我只需要从父 python 类继承属性,但我需要能够调用整个类链一次...所以,我正在寻找这样的东西.
我正在努力兑现 promise ,到目前为止我偶然发现了这一点: new Promise((resolve, reject) => { setTimeout(() => { r
我试图理解 promise ,我需要链接它们并装饰来自不同端点的对象宽度数据。 例如: 我的 Node-express 应用程序中有这个 //controller.js export const ge
我是一名优秀的程序员,十分优秀!