我感兴趣的是这两行代码是否相同:
shared_ptr<int> sp(new int(1)); // double allocation?
shared_ptr<int> sp(make_shared<int>(1)); // just one allocation?
Run Code Online (Sandbox Code Playgroud)
如果这是真的,有人可以解释为什么在第二行只有一个分配?
我正在玩,gcc
并尝试了以下一些代码:
int A = 42;
int *B = &A;
int *C = &*B;
Run Code Online (Sandbox Code Playgroud)
并且C == &A
,正如所料.但是当我尝试:
int *B = NULL;
int *C = &*B;
Run Code Online (Sandbox Code Playgroud)
原来C == NULL
,没有段错误.因此,在获取其地址之前&*B
实际上并未解除引用B
.
我的猜测是预处理器剥离出来的情况下,&*
和*&
之前,他们甚至得到了编译器,因为他们否定对方,但我无法找到任何文件,以验证这是否是标准ç或编译器特定的.
被预处理器剥离出来&*
,并*&
和我可以期待从任何给定的编译器这种行为?
我有一些简单的代码,但我收到一个警告:
-bash-3.2$ gcc -Wall print_process_environ.c -o p_p
print_process_environ.c: In function 'print_process_environ':
print_process_environ.c:24: warning: implicit declaration of function 'strlen'
print_process_environ.c:24: warning: incompatible implicit declaration of built-in function 'strlen'
Run Code Online (Sandbox Code Playgroud)
以下是代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <strings.h>
void
print_process_environ(pid_t pid)
{
int fd;
char filename[24];
char environ[1024];
size_t length;
char *next_var;
snprintf(filename, sizeof(filename), "/proc/%d/environ", (int)pid);
printf("length of filename: %d\n", strlen(filename));
fd = open(filename, O_RDONLY);
......
Run Code Online (Sandbox Code Playgroud)
定义strlen()
是:
#include <string.h>
size_t strlen(const char …
Run Code Online (Sandbox Code Playgroud) 假设我想引用initializer_list
我已定义的成员.我可以做吗?
这段代码在Visual Studio和gcc中编译并给出了预期的"13 55" ,我只想知道它是合法的:
const int foo[2] = {13, foo[0] + 42};
Run Code Online (Sandbox Code Playgroud) c++ arrays initializer-list language-lawyer aggregate-initialization
我的意思是为什么std::make_tuple
存在?我知道在某些情况下,该函数会减少您必须键入的字符数,因为您可以避免使用模板参数.但这是唯一的原因吗?std::tuple
当其他类模板没有这样的功能时,该功能存在的特殊之处是什么?是否只是因为你可能std::tuple
在这种情况下更频繁地使用?
以下是两个std::make_tuple
减少字符数量的示例:
// Avoiding template parameters in definition of variable.
// Consider that template parameters can be very long sometimes.
std::tuple<int, double> t(0, 0.0); // without std::make_tuple
auto t = std::make_tuple(0, 0.0); // with std::make_tuple
// Avoiding template parameters at construction.
f(std::tuple<int, double>(0, 0.0)); // without std::make_tuple
f(std::make_tuple(0, 0.0)); // with std::make_tuple
Run Code Online (Sandbox Code Playgroud)
但是,如上所述,对于许多其他类模板,您没有这样的函数.
我在编译一些可移植代码时碰到了这个gcc
.基本上这个奇怪的代码在Visual Studio中编译,这真的让我大吃一惊:
class Zebra {int x;};
Zebra goo() {Zebra z; return z;}
void foo(Zebra &x)
{
Zebra y;
x = y;
foo(goo());
}
Run Code Online (Sandbox Code Playgroud)
Visual studio
让这一个飞.gcc
将捕获此作为编译错误.有趣的是,如果你输入def Zebra为int,VC++
会抱怨.相当矛盾的行为.思考?
以下代码
#include <threads.h>
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
fatal error: threads.h: No such file or directory
Run Code Online (Sandbox Code Playgroud)
使用最新的GCC和Clang -std = c11.
GCC和Clang不支持C11线程吗?或者是否有一个黑客(或安装的东西)来获得它?我只是使用Ubuntu 14.04和Ubuntu repo中的gcc和clang包.
正如文件所说:
如果这不是一个的影响是不确定
short
,int
,long
,long long
,unsigned short
,unsigned int
,unsigned long
,或unsigned long long
.
如果我不关心范围,我可以掩盖更大类型的位来生成随机数.如果没有,那就更复杂了.为什么默认情况下不提供字节类型?
如何将a的第一个n
元素复制或移动std::vector<T>
到C++ 11中std::array<T, n>
?
c++ ×6
c ×4
c++11 ×4
standards ×2
addressof ×1
arrays ×1
c11 ×1
containers ×1
indirection ×1
make-shared ×1
posix ×1
random ×1
stdtuple ×1