小编Sha*_*our的帖子

使用make_shared时会发生什么

我感兴趣的是这两行代码是否相同:

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)

如果这是真的,有人可以解释为什么在第二行只有一个分配?

c++ smart-pointers make-shared c++11

26
推荐指数
2
解决办法
4414
查看次数

C预处理器是否删除了"&*"的实例?

我正在玩,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.

我的猜测是预处理器剥离出来的情况下,&**&之前,他们甚至得到了编译器,因为他们否定对方,但我无法找到任何文件,以验证这是否是标准ç或编译器特定的.

被预处理器剥离出来&*,并*&和我可以期待从任何给定的编译器这种行为?

c standards addressof indirection c-preprocessor

25
推荐指数
2
解决办法
1208
查看次数

接收警告"隐含声明功能'strlen'"

我有一些简单的代码,但我收到一个警告:

-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)

c posix

24
推荐指数
2
解决办法
5万
查看次数

我可以引用初始化列表的先前成员吗?

假设我想引用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

24
推荐指数
1
解决办法
558
查看次数

`std :: make_tuple`的原因是什么?

我的意思是为什么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)

但是,如上所述,对于许多其他类模板,您没有这样的函数.

c++ c++11 stdtuple

24
推荐指数
2
解决办法
7127
查看次数

非const引用绑定到临时,Visual Studio错误?

我在编译一些可移植代码时碰到了这个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++会抱怨.相当矛盾的行为.思考?

c++ visual-studio temporary-objects

23
推荐指数
2
解决办法
2611
查看次数

找不到C11 GCC threads.h?

以下代码

#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包.

c multithreading c11

23
推荐指数
2
解决办法
2万
查看次数

C标准是否有缺陷报告的网站?

在我之前的问题中,讨论似乎意味着C标准可能存在缺陷,最重要的回答者的最后一句话进一步暗示:

该标准的作者只是忽略了这样说.

我知道C++有一个在线网站,您可以在其中搜索缺陷报告,即这里是我对空指针常量的搜索,但C标准是否有相似之处?

c standards language-lawyer

23
推荐指数
2
解决办法
746
查看次数

为什么不是`std :: uniform_int_distribution <uint8_t>`和`std :: uniform_int_distribution <int8_t>`允许?

正如文件所说:

如果这不是一个的影响是不确定short,int,long,long long,unsigned short,unsigned int,unsigned long,或unsigned long long.

如果我不关心范围,我可以掩盖更大类型的位来生成随机数.如果没有,那就更复杂了.为什么默认情况下不提供字节类型?

c++ random c++11

23
推荐指数
1
解决办法
2311
查看次数

将std :: vector复制到std :: array中

如何将a的第一个n元素复制或移动std::vector<T>到C++ 11中std::array<T, n>

c++ containers c++11

22
推荐指数
2
解决办法
1万
查看次数