我对这个代码的MSVC和clang之间的行为差异感到困惑:
#include <iostream>
#include <cstdint>
int main() {
int64_t wat = -2147483648;
std::cout << "0x" << std::hex << wat << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2010,2012和2013(在Windows 7上)全部显示:
0x80000000
Run Code Online (Sandbox Code Playgroud)
但是clang 503.0.40(在OSX上)显示:
0xffffffff80000000
Run Code Online (Sandbox Code Playgroud)
根据C++标准,正确的行为是什么?文字应该是零扩展还是符号扩展到64位?
我知道这int64_t wat = -2147483648LL;会在两个编译器上产生相同的结果,但我想知道没有文字后缀的正确行为.
给定1和0的向量,我想计算值为1的条目数.但是,向量可能很长,我只关心向量是否有零个,一个或多个值为1的条目.
使用这里给出的方法,我可以计算向量中的数量.
(count (filter #{1} [1 0 1 0 0 1 1]))
在这种情况下,我可以限制过滤器(或使用其他方法)以避免访问向量的任何三个以上的元素吗?
我想使用C++ 17结构化绑定为类成员变量赋值,如下所示:
#include <cmath>
#include <iostream>
struct Result {
double value;
bool error;
};
Result square_root(double input) { return {std::sqrt(input), input < 0}; }
struct Calculator {
double result_;
bool error_;
public:
void ComputeSquareRoot(double input) {
[ result_, error_ ] = square_root(input);
}
void DisplayResult() {
if (error_)
std::cout << "Cannot take the square root of a negative number.\n";
else
std::cout << "The square root is " << result_ << ".\n";
}
};
int main(int argc, char* argv[]) {
Calculator …Run Code Online (Sandbox Code Playgroud)