从const变量中删除const的AFAIK是未定义的行为:
const int i = 13;
const_cast<int&>(i) = 42; //UB
std::cout << i << std::endl; //out: 13
Run Code Online (Sandbox Code Playgroud)
但是const函数参数是"真正的"常量吗?让我们考虑以下示例:
void foo(const int k){
const_cast<int&>(k) = 42; //UB?
std::cout << k << std::endl;
}
int main(){
foo(13); //out: 42
}
Run Code Online (Sandbox Code Playgroud)
好像编译器不一样优化适用于const int k作为const int i.
在第二个例子中有UB吗?是否const int k帮助编译器优化代码或编译器只检查const正确性,仅此而已?
我有一些无法更改或重建的静态库。该库使用全局变量。像这样:
//lib A
#include <iostream>
static int i = 0;
void printA(){
std::cout << i++ << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我想创建两个共享库,它们具有自己的静态库“副本”及其全局状态:
//lib B
#include "liba.h"
void printB(){
printA();
}
Run Code Online (Sandbox Code Playgroud)
????????????
//lib C
#include "liba.h"
void printC(){
printA();
}
Run Code Online (Sandbox Code Playgroud)
...并同时使用它们:
#include "libb.h"
#include "libc.h"
int main(){
printB();
printB();
printC();
printC();
}
Run Code Online (Sandbox Code Playgroud)
我期望以下输出:
0
1
0
1
Run Code Online (Sandbox Code Playgroud)
..但实际上得到:
0
1
2
3
Run Code Online (Sandbox Code Playgroud)
好像libB和libC共享公共计数器变量。如果可以访问libA源代码,我将使用进行重建-fvisibility=hidden。但是不幸的是我只有二进制文件。
有没有办法实现预期的行为而无需libA重建?
似乎 xcodebuild 将所有内容打印到标准输出。
$ /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project test.xcodeproj build -target test -configuration Debug -jobs 3 2>err
# xcodebuild stdout with bunch of warnings and errors
$ cat err
** BUILD FAILED **
The following build commands failed:
CompileC _build/test.build/Objects-normal/x86_64/test.o /Users/me/test/test.cpp normal x86_64 objective-c++ com.apple.compilers.llvm.clang.1_0.compiler
(1 failure)
Run Code Online (Sandbox Code Playgroud)
这不允许我的 IDE(QtCreator)正确解析输出。
实际上,clang 会将错误和警告打印到 stderr。但是 xcodebuild 由于某种原因将所有内容重定向到 strdout。有没有办法让 xcodebuild 打印错误和警告到 stderr except 1>&2?
我有一个不能直接使用的类模板,只允许特化。我想用来static_assert显示有意义的错误消息。我不能只输入,static_assert(false, "error");因为false它不依赖于值,即使模板从未使用过,编译器也可能会显示错误消息。
我的解决方案:
template<class>
struct AlwaysFalse : std::false_type{};
#define DEPENDENT_FALSE(arg) AlwaysFalse<decltype(sizeof(arg))>::value
template<class T>
struct Foo{
static_assert(DEPENDENT_FALSE(T), "You must use specialization!");
};
template<int i>
struct Bar{
static_assert(DEPENDENT_FALSE(i), "You must use specialization!");
};
Run Code Online (Sandbox Code Playgroud)
但我不确定实现DEPENDENT_FALSE。由于MSVC不把sizeof(arg)作为模板依赖性表达(不像GCC),不过decltype(sizeof(arg)) 是罚款。
有人可以用标准来解释这种行为吗?便携吗?
file t.csv:
a ; b ; c ; d
1 ; 2 ; NA; 4
5 ; NA; 6 ; 7
Run Code Online (Sandbox Code Playgroud)
我读了t.csv文件
> t <- read.table("t.csv",header = T, sep = ";")
Run Code Online (Sandbox Code Playgroud)
我期待的是:
> str(t)
'data.frame': 2 obs. of 4 variables:
$ a: num 1 5
$ b: num 2 NA
$ c: num NA 6
$ d: num 4 7
Run Code Online (Sandbox Code Playgroud)
我得到了什么
> str(t)
'data.frame': 2 obs. of 4 variables:
$ a: num 1 5
$ b: Factor w/ 2 levels …Run Code Online (Sandbox Code Playgroud) 我经常看到关于省略号的教程,例如:
void foo(int i, ...){
int *p = &i;
++p;
//...
}
Run Code Online (Sandbox Code Playgroud)
而我只是想知道,这种方法在标准方面是否正确?我可以使用没有va_*宏的可变参数吗?也许某些实现以相反的顺序存储args,或类似的东西.
UPD: "使用"=可移植且可靠地传递args
c++ ×4
c ×2
c++11 ×1
compilation ×1
csv ×1
gcc ×1
linker ×1
linux ×1
qt-creator ×1
r ×1
standards ×1
templates ×1
xcode ×1
xcodebuild ×1