小编Cas*_*eri的帖子

使用dynamic_pointer_cast时无法进行动态转换

为什么这段代码不起作用?

std::shared_ptr<Event> e = ep->pop();
std::shared_ptr<TrackerEvent> t;

t = std::dynamic_pointer_cast<TrackerEvent>(e);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

/usr/include/c++/4.6/bits/shared_ptr.h:386: error: cannot dynamic_cast '(& __r)->std::shared_ptr<Event>::<anonymous>.std::__shared_ptr<_Tp, _Lp>::get [with _Tp = Event, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]()' (of type 'class Event*') to type 'class TrackerEvent*' (source type is not polymorphic)
Run Code Online (Sandbox Code Playgroud)

TrackerEvent继承自Event所以我猜问题是我不能朝这个方向投.但是ep->pop()可能会返回一个类型的对象EventTrackerEvent.我希望,当我尝试将其转换为TrackerEvent并返回NULL我想知道我是否有一个EventTrackerEvent...

我该怎么办?

c++ dynamic-cast shared-ptr c++11

5
推荐指数
1
解决办法
2543
查看次数

在比较中,GCC 似乎更喜欢小的即时值。有没有办法避免这种情况?

首先是一个微不足道的数学事实:给定整数nm,我们有n < m当且仅当n <= m - 1

GCC 似乎更喜欢较小绝对值的即时值。因此,当m已知并且满足其他条件时,编译器在等效比较表达式中选择最小化绝对值的表达式。例如,它喜欢n <= 1000n < 1001和GCC 9.2将这种

bool f(uint32_t n) {
  return n < 1001;
}
Run Code Online (Sandbox Code Playgroud)

进入这个x86汇编代码

f(unsigned int):
  cmpl $1000, %edi
  setbe %al
  ret
Run Code Online (Sandbox Code Playgroud)

这可能有很好的性能原因,但这不是我的问题。我想知道的是:有没有办法强制 GCC 保持原始比较?更具体地说,我不担心可移植性,因此,GCC 细节(选项、编译指示、属性等)对我来说是可以的。但是,我正在寻找一个constexpr友好的解决方案,它似乎排除了 inline asm。最后,我的目标是 C++17,它不包括std::is_constant_evaluated. (话虽如此,请尽管不顾我的限制自由地提供答案,因为它可能对其他人仍然有用。)

你可能会问我为什么要做这样的事情。开始了。据我所知(如果我错了,请纠正我)这种行为可能是x86_64以下示例中的“悲观化” :

bool g(uint64_t n) {
  n *= 5000000001;
  return n …
Run Code Online (Sandbox Code Playgroud)

c++ x86 assembly gcc

5
推荐指数
1
解决办法
176
查看次数

将MinGW与Visual Studio 2010集成(Makefile项目)

我正在尝试使用Visual Studio 2010的MinGW(版本4.7.2)来使用一些新的C++ 11功能(遗憾的是我仍然使用WindowsXP并且不能使用Visual Studio 2012).首先,我创建了一个项目:File - > New Project - > Visual C++ - > General - > Makefile-Project

General:
Build Command Line: mingw32-make.exe
Rebuild All Command Line: mingw32-make.exe rebuild all
Clean Command Line: mingw32-make.exe clean all

IntelliSense:
Include Search Path: C:\MinGW\lib\gcc\mingw32\4.7.2\include\c++;C:\MinGW\include;
Assembly Search Path: C:\MinGW\lib\gcc\mingw32\4.7.2;C:\MinGW\lib;
Additional Arguments: -std=c++11
Run Code Online (Sandbox Code Playgroud)

我创建了一个包含内容的makefile:

all:
    g++ -std=c++11 -pthread -o Makefile_Test.exe main.cpp
Run Code Online (Sandbox Code Playgroud)

它编译得很好,但在Visual Studios编辑器中几乎所有东西都是波浪形的红色.即

std::vector<std::thread> threads;
Run Code Online (Sandbox Code Playgroud)

std::vector - >'错误:命名空间std没有成员向量'

std::thread - >'错误:命名空间std没有成员线程'

甚至 std::cout << "";

std::cout - >'错误:命名空间std没有成员cout'

但是我当然包括了相应的标题:Visual Studio甚至可以找到它们(将光标放在#include - > Ctrl …

c++ mingw visual-studio-2010 makefile-project c++11

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

将函数返回的共享指针绑定到对值的左值引用是不错的做法?

虽然我花了一段时间才习惯它,但我现在养成了让我的函数通过lvalue-reference const而不是value 来获取共享指针参数的习惯(除非我需要修改原始参数,当然,在这种情况下)我把它们通过左值引用非const):

void foo(std::shared_ptr<widget> const& pWidget)
//                               ^^^^^^
{
    // work with pWidget...
}
Run Code Online (Sandbox Code Playgroud)

这具有避免共享指针的不必要副本的优点,这意味着线程安全地增加引用计数并且可能引起不期望的开销.

现在我一直想知道采用一种有点对称的习惯来检索由函数的值返回的共享指针是否合理,比如下面的代码片段末尾:

struct X
{
    // ...
    std::shared_ptr<Widget> bar() const
    {
        // ...
        return pWidget;
    }
    // ...
    std::shared_ptr<Widget> pWidget;
};

// ...

// X x;
std::share_ptr<Widget> const& pWidget = x.bar();
//                     ^^^^^^
Run Code Online (Sandbox Code Playgroud)

采用这种编码习惯有任何陷阱吗?有没有什么理由我通常会更喜欢将返回的共享指针分配给另一个共享指针对象而不是将它绑定到引用?

c++ coding-style reference shared-ptr c++11

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

为什么不能使用auto作为模板类型参数(例如std :: array <auto,5>)?

为什么不允许这样做,例如:

std::array<auto, 5> myArray {

};
Run Code Online (Sandbox Code Playgroud)

它会让我的生活变得如此简单,因为我可以在数组中存储多个数据类型.我确信这是一个合乎逻辑的解释,只是想知道它是什么.

c++ stl c++11

4
推荐指数
3
解决办法
1057
查看次数

是否可以从父类"转发任何类函数"的C++模板?

class Foo {
public:
  void methodA();
};

class ManagedFoo {
Foo fooInst;
public:
  void methodA() { doSomething(); fooInst.methodA();}
};
Run Code Online (Sandbox Code Playgroud)

现在我想将ManagedFoo作为模板,不仅管理任何类Foo,而且在调用任何Foo函数之前,首先调用doSomething.

template<typename _TyManaged>
class Manager {
  _TyManaged _managedInst;
  void doSomething();
public:
  /*Forward every function called by _managedInst*/
  /*How to write this?*/
};
Run Code Online (Sandbox Code Playgroud)

我想让它变得一样,让它在这两个类之间可以替换,如下所示:

Foo* foo = new Foo();
foo->methodA();

Manager<Foo> managedFoo = new Manager<Foo>();
managedFoo->methodA(); //Hope it call Manager::doSomething() first then call _managedInst.methodA();
Run Code Online (Sandbox Code Playgroud)

C++ 11模板可以做这样的事吗?如果答案是肯定的,怎么办?

c++ templates forwarding c++11

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

函数双重到std :: move?

假设我有一个只有一个构造函数的类:

class T {
 public:
  T(BigClass&& big) : big(std::move(big)) {}
  ...

  SomeBigClass
};
Run Code Online (Sandbox Code Playgroud)

在大多数地方,构造函数在temporaries上调用,但在一个地方我需要制作BigClass的显式副本,因为它不是临时的,并且将在循环中多次使用:

void foo(const BigClass& big) {
  while (...) {
    T t(std::make_a_copy(big));
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

std::move在C++ 11或C++ 14中是否有任何"双重"功能可以取代上面的make_a_copy?

编辑:一些澄清.

c++ move-semantics c++11

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

使用fscanf读取double

我想从文本文件中读取双倍,例如31 39.9316476397222 116.113516352222

我尝试了两种,而不是工作.我只能读取前几个十进制数字例如39.93164而不是39.9316476397222谁知道为什么?谢谢!

int NodeID;
double _lat,_long;
fscanf (pFile, "%d %lf %lf", &NodeID,&_lat,&_long);
printf ("I have read: %d %f %f\n", NodeID,_lat,_long);

fscanf (pFile, "%d %lf %lf", &NodeID,&_lat,&_long);
printf ("I have read: %d %lf %lf\n", NodeID,_lat,_long);
Run Code Online (Sandbox Code Playgroud)

c c++ file scanf

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

具有相似索引的值的总和

假设我在一个文本文件中有一组1000个统计数据.其第一列表示索引的数量,第二列表示该值的值.索引可以重复,相应的值可以不同.我想计算索引的出现次数和每个索引的值的总和.

我编写了一个代码,它给出了索引出现的结果,但它没有给出相应的值总和.

假设我的文本文件有一组这样的数据 -

#index   value
  4      0.51
  5      0.13
  5      0.53
  5      0.25
  6      0.16
  6      0.16
  7      0.38
  4      0.11
  3      0.101
  4      0.32
  4      0.2 ... and more
Run Code Online (Sandbox Code Playgroud)

所以在这种情况下 -

指数4 出现 4次,相应的值之和 =(0.51 + 0.11 + 0.32 + 0.2)= 1.14

同样

指数5 出现 2次,值之 =(0.13 + 0.53)= 0.66等.

我的守则

这是我的代码 -

#include <iostream>
#include <map>
#include <fstream>

using namespace std;


int main()
{
    map<double,double>   index;
    double  number,value;
    double total;


    ifstream theFile ("a1.txt");
    while(theFile …
Run Code Online (Sandbox Code Playgroud)

c++ io c++11

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

可以在派生类中运算符<<在c ++的基类中调用另一个运算符<<吗?

在我的代码中,Manager派生自Employee并且每个都有一个operator<<覆盖.

class Employee{
protected:
    int salary;
    int rank;
public:
    int getSalary()const{return salary;}
    int getRank()const{return rank;}
    Employee(int s, int r):salary(s), rank(r){};
};
ostream& operator<< (ostream& out, Employee& e){
    out << "Salary: " << e.getSalary() << " Rank: " << e.getRank() << endl;
    return out;
}

class Manager: public Employee{
public:
    Manager(int s, int r): Employee(s, r){};
};
ostream& operator<< (ostream& out, Manager& m){   
    out << "Manager: ";
    cout << (Employee)m << endl;  //can not compile, …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading derived-class operator-keyword

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