示例:这是合法的C++ 14吗?
#include <iostream>
static int d() {
return 42;
}
static int e(int d = d()) {
return d;
}
int main() {
std::cout << e() << " " << e(-1) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
g ++ 5.4 -std=c++14喜欢它,但clang ++ 3.8 -std=c++14抱怨:
samename.cxx:3:23: error: called object type 'int' is not a function or function pointer
static int e(int d = d()) {return d;}
~^
Run Code Online (Sandbox Code Playgroud) 我有一个传统的C lib,而function(setsockopts)想要一个指针参数.在C++ 11(gcc 4.8)中,我可以在不初始化命名变量的情况下传递此参数吗?
我有以下不满意的解决方案:
#include <iostream>
#include <memory>
int deref(int const * p) {return * p;}
using namespace std;
int main() {
int arg = 0; cout << deref(& arg) << endl;
// works, but is ugly (unnecessary identifier)
cout << deref(& 42) << endl;
// error: lvalue required as unary ‘&’ operand
cout << deref(& * unique_ptr<int>(new int(42))) << endl;
// works, but looks ugly and allocates on heap
}
Run Code Online (Sandbox Code Playgroud) 假设a,b,c,和d声明double(或float).以下表达式是否始终为真?
! ( (a >= b) && (c <= d) ) || ( (a-c) >= (b-d) )
! ( (a > b) && (c <= d) ) || ( (a-c) > (b-d) )
! ( (a >= b) && (c < d) ) || ( (a-c) > (b-d) )
Run Code Online (Sandbox Code Playgroud)
IEEE 754或当前的C或C++标准是否有任何保证?任何编译器都会在编译时将其优化为真的吗?我主要对正常值感兴趣,而不是在低于正常值或特殊值.
在我看来,这应该主要取决于减法期间的舍入误差.
在哪里可以获得明确的答案,我memcpy(使用Ubuntu附带的eglibc实现)是否是线程安全的? - 老实说,我真的没有在文档中找到明确的YES或NO.
顺便说一句,对于"线程安全",我的意思是,memcpy只要同时复制字节的日期字节是安全的,就可以安全地同时使用.至少如果将只读数据复制到不重叠的区域,这应该是可能的.
理想情况下,我想看到类似的名单在底部此页面中ARM编译器的文档.
在标题中说,我不想自己读,但我确实包括,我有
#define A B
#define B C
Run Code Online (Sandbox Code Playgroud)
现在
#define STR(name) # name
Run Code Online (Sandbox Code Playgroud)
定义一个宏,它给我任何宏的名称作为字符串,和
#define EXP_STR(name) STR(name)
Run Code Online (Sandbox Code Playgroud)
定义了一个宏,它可以将任何宏的完全扩展作为字符串.所以
cout << STR(A) << EXP_STR(A) << endl;
Run Code Online (Sandbox Code Playgroud)
将打印AC.
有没有办法"B"从A使用一些宏?
下面的长显式初始化列表是否可以被生成它的某个模板替换?
std::array<Foo, n_foos> foos = {{
{0, bar},
{1, bar},
{2, bar},
{3, bar},
{4, bar},
{5, bar},
{6, bar},
{7, bar},
}};
Run Code Online (Sandbox Code Playgroud)
现在这里的代码只是因为我们有了constexpr int n_foos = 8.怎么能做到任意和大n_foos?
IAR Embedded C编译器对此很满意,我认为它是正确的C代码:
struct incomplete;
typedef struct incomplete (*why_not)[2];
struct incomplete {struct incomplete *known_to_work;} array[2];
why_not ok = &array;
Run Code Online (Sandbox Code Playgroud)
但是,gcc和clang choke对定义why_not:
incomplete.c:2:29: error: array type has incomplete element type ‘struct incomplete’
typedef struct incomplete (*why_not)[2];
^
Run Code Online (Sandbox Code Playgroud)
从技术上讲,没有理由拒绝"指向不完整数组的指针"的定义.毕竟,只有在取消引用这样的变量或执行某些指针算术时才需要结构定义.
我渴望在可能的情况下隐藏结构定义.
C标准对此有何看法?
我尝试了一些gcc编译器来查看默认的枚举大小是短(至少一个字节,如强制-fshort-enums)还是不短(至少4个字节,强制使用-fno-short-enums):
user@host:~$ echo '_Static_assert(4 == sizeof(enum{E}), "enum size is not 4");' | x86_64-linux-gnu-gcc -fsyntax-only -xc - && echo "OK, enum size is 4 on x86_64-linux-gnu"
OK, enum size is 4 on x86_64-linux-gnu
user@host:~$ echo '_Static_assert(4 == sizeof(enum{E}), "enum size is not 4");' | arm-linux-gnueabihf-gcc -fsyntax-only -xc - && echo "OK, enum size is 4 on arm-linux-gnueabihf"
OK, enum size is 4 on arm-linux-gnueabihf
user@host:~$ echo '_Static_assert(4 == sizeof(enum{E}), "enum size is not 4");' | /opt/Atollic_TrueSTUDIO_for_STM32_x86_64_9.1.0/ARMTools/bin/arm-atollic-eabi-gcc -fsyntax-only …Run Code Online (Sandbox Code Playgroud) mkdir -p /tmp/build &&
cd /tmp/build &&
mkdir -p /tmp/src &&
echo "int main(){return 0;}" > /tmp/src/prog.c &&
gcc --coverage -o prog /tmp/src/prog.c &&
./prog &&
gcovr -v -r .
Run Code Online (Sandbox Code Playgroud)
将输出一个空报告。
Scanning directory . for gcda/gcno files...
Found 2 files (and will process 1)
Processing file: /tmp/build/prog.gcda
Running gcov: 'gcov /tmp/build/prog.gcda --branch-counts --branch-probabilities --preserve-paths --object-directory /tmp/build' in '/tmp/build'
Finding source file corresponding to a gcov data file
currdir /tmp/build
gcov_fname #tmp#src#prog.c.gcov
[' -', ' 0', 'Source', '/tmp/src/prog.c\n']
source_fname /tmp/build/prog.gcda
root …Run Code Online (Sandbox Code Playgroud) 信不信由你,我想static_assert在宏扩展到指定的初始化器:
#define INIT(N) \
/* static_assert((N) < 42, "too large"), */ \
[(N)] = (N)
int array[99] = { INIT(1), INIT(2), INIT(42) };
Run Code Online (Sandbox Code Playgroud)
我想要一个错误INIT(42),但取消注释static_assert是一个语法错误.AFAIK static_assert在语法上是一种声明.我如何在这个例子中使用它?