小编P.W*_*P.W的帖子

1000个共享指针占用多少内存?

  1. (例如)1000个共享指针占用多少内存?
  2. 是16 x 1000字节吗?
  3. 32位和64位系统是否有所不同?

c++ smart-pointers shared-ptr c++11

4
推荐指数
1
解决办法
244
查看次数

如何使用static_cast来解决重载函数?

  void hello()
  {
    cout << "helloworld" << endl;
  }

  void hello(string s)
  {
    cout << "hello " << s << endl;
  }

  void doWork()
  {
    thread t1(static_cast<void ()>(&hello));
    thread t2(static_cast<void (string)>(&hello),"bala");
    t1.join();
    t2.join();
  }
Run Code Online (Sandbox Code Playgroud)

错误:

thread.cc|19 col 42| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void()'                                                          
thread.cc|20 col 48| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void(std::string) {aka void(std::basic_string<char>)}'
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用typedef函数指针或lambda.是不是可以使用static_cast

c++

3
推荐指数
1
解决办法
3315
查看次数

在下面的例子给出的结构有什么不幸?

"15.6.2初始化基础和成员"(N4713)部分在第11项之后有以下示例:

struct A {
    A() = default; // OK
    A(int v) : v(v) { } // OK
    const int& v = 42; // OK
};
A a1; // error: ill-formed binding of temporary to reference
A a2(1); // OK, unfortunately
Run Code Online (Sandbox Code Playgroud)

关于这个例子最后一行的构造有什么不幸

我在整个参考文献中搜索了其他可能发生的"不幸"行为,但我找不到.

如果在这种特殊情况下不幸,那么它是否可能被视为非法?

c++ constructor reference temporary-objects

3
推荐指数
1
解决办法
100
查看次数

如果文件打开,fopen() 返回文件指针什么?

例如,如果我们声明一个文件指针fp并打开一个文件,如下所示:
FILE* fp = fopen("filename","w");

如果文件未打开,fopen则返回NULL到文件指针fp。如果文件打开,文件指针中存储什么fp

c file-pointer

3
推荐指数
1
解决办法
2754
查看次数

为什么C ++不支持“嵌套注释”?

在C ++中,多行注释以开头/*和结尾*/

但是,如果发生以下情况,则会导致编译错误

/*
int a = 20;
  /*
   int b = 10;

  */ 
*/
Run Code Online (Sandbox Code Playgroud)

有什么原因为什么C ++不支持这种“嵌套注释”样式?

c++ compiler-errors

3
推荐指数
1
解决办法
242
查看次数

为什么不是条件 *First1 == * First2 ?? (C++ STL 包括函数实现)

我是一名学习 STL C++ 的学生,有一个简单的问题。

我在学习如何在算法库中实现包含函数时想知道。请参阅此代码(此代码来自https://en.cppreference.com/w/cpp/algorithm/includes

template<class InputIt1, class InputIt2> 
bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) {
    for (; first2 != last2; ++first1) {
        if (first1 == last1 || *first2 < *first1)
            return false;
        if ( !(*first1 < *first2) )
            ++first2;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

本节有问题。

if ( !(*first1 < *first2) )
            ++first2;
Run Code Online (Sandbox Code Playgroud)

这部分表现为判断是否*first1等于的代码,*first2以确定给定的部分序列是否是有序序列的一部分。我认为。

如果是这样,*first1 == * first2似乎足够了,我想知道为什么!(first1 < first2)

这种情况有什么特殊原因吗??

c++ algorithm stl

3
推荐指数
1
解决办法
92
查看次数

增加月份行为的原理是什么?

增加月份的示例

28/02/2009 + 1个月= 2009年3月31日

#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/gregorian/formatters.hpp>
#include <iostream>
using namespace boost::gregorian;
using namespace std;

int main()
{
typedef boost::date_time::month_functor<date> add_month;
        date d(2019, Feb, 28);
        add_month mf(1);
        date d2 = d + mf.get_offset(d);
        cout << to_simple_string(d2) << endl;//output:2019-Mar-31
}
Run Code Online (Sandbox Code Playgroud)

输出为2019年3月31日而不是2019年3月28日。这种行为的理论基础是什么?谈论它以类似的添加月份代码在C#和delphi中输出2019年3月28日。

c++ boost date

3
推荐指数
1
解决办法
61
查看次数

Function interleaving in pre C++17

Look at this simple function call:

f(a(), b());
Run Code Online (Sandbox Code Playgroud)

According to the standard, call order of a() and b() is unspecified. C++17 has the additional rule which doesn't allow a() and b() to be interleaved. Before C++17, there was no such rule, as far as I know.

Now, look at this simple code:

int v = 0;

int fn() {
    int t = v+1;
    v = t;
    return 0;
}

void foo(int, int) { }

int main() {
    foo(fn(), fn());
} …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++11 c++14 c++17

3
推荐指数
1
解决办法
187
查看次数

std :: gcd无法在g ++ 5.4.0中编译-'gcd'不是'std'的成员

环境:

  • Ubuntu 16.04 64位
  • g ++版本5.4.0

这是代码:

#include <numeric>
...
auto g = std::gcd(10, 4);
...
Run Code Online (Sandbox Code Playgroud)

我已经打开-std=c++17了编译命令中的选项:

g++ -m64 -std=c++17   -c -g -w -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
Run Code Online (Sandbox Code Playgroud)

然后我得到了错误:

错误:“ gcd”不是“ std”的成员

从此网页开始std::gcd自C ++ 17开始引入。

此网页上,我的g ++版本支持C ++ 17。

但是为什么仍然存在错误?相同的代码可以在Visual Studio 2017中编译而不会出现任何错误。

c++ g++ std greatest-common-divisor c++17

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

使用概念的部分专业化

我只是在阅读 C++20 概念的例子。现在我正在尝试创建一个函数,该函数将打印出给定类型是否是哈希表或不使用与部分专业化混合的概念。但不幸的是它不起作用。

#include <iostream>
#include <string>

template<typename T>
concept Hashtable = requires(T a) {
    { std::hash<T>{}(a) } -> std::size_t;
};

struct Foo {};

template <typename T>
void Bar() {
    std::cout << "Type T is not a hashtable" << std::endl;
}

template <Hashtable T>
void Bar<T> {
    std::cout << "Type T is a hashtable" << std::endl;
}

int main()
{
    Bar<Foo>();
    Bar<std::string>();
}
Run Code Online (Sandbox Code Playgroud)

我正在使用编译器版本 GCC HEAD 9.0.1,编译器标志是g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.69.0/gcc-head/include -std=gnu++2a "-fconcepts". 它给了我以下编译器错误:

prog.cc:18:6: error: template-id 'Bar<T>' …
Run Code Online (Sandbox Code Playgroud)

c++ partial-specialization c++-concepts c++20

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