小编yan*_*ano的帖子

g ++警告:从int转换为uint16_t可能会改变其值

在高级别SO用户的建议下,我最近开始使用-Wconversion我的代码库上的标志进行编译.这产生了很多警告,其中一些是合法的(例如,不必要的添加signedunsigned类型),但也有一些头部刮擦,如下所示:

#include <cstdint>

int main()
{
  uint16_t a = 4;
  uint16_t b = 5;

  b += a;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译时g++ -Wconversion -std=c++11 -O0 myFile.cpp,我得到了

warning: conversion to 'uint16_t {aka short unsigned int}' from 'int' may alter its value [-Wconversion]
    b += a;
      ^
Run Code Online (Sandbox Code Playgroud)

我细读上SO(处理一些类似的问题|<<运营商),采取一看这里,并已阅读了数字升级和数值转换部分在这里.我的理解是,为了进行数学计算,ab被提升为int(因为这是第一种可以适应整个uint16_t值范围的类型),执行数学运算,结果被写回...除了数学结果是一个int并写回来uint16_t生成警告.其他问题的共识基本上是抛弃了警告,而我唯一能想出如何做到这一点的方法就是b = (uint16_t)(b + a);(或等价的b …

c++ g++

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

重载operator = break std :: sort

可能是一个骗局,但我找不到它.

在敲击键盘两天之后,我发现重载equals运算符(operator=)显然会中断std::sort.也许我的输operator=错不正确?这是我的MCVE:

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <cstdint>
#include <vector>

struct Person
{
  std::string name;
  uint32_t age;

  bool operator< (const Person& p)
  {
    return this->age < p.age;
  }

  Person operator= (const Person& p)
  {
    Person newP;
    newP.name = p.name;
    newP.age = p.age;

    return newP;
  }

  static bool SortPeople(const Person& p1, const Person& p2)
  {
    return p1.age < p2.age;
  }
};

void PrintPeople(const std::vector<Person>& people)
{
  std::cout << "============ people begin" …
Run Code Online (Sandbox Code Playgroud)

c++ assignment-operator c++11

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

这里逻辑运算符如何短路?

#include <stdio.h>

int main()
{
   int x = -2;
   while (x++ || x==0)
   {
      printf("X");
   }
}
Run Code Online (Sandbox Code Playgroud)

输出如下

XX

为什么?

我期望这段代码进入无限循环,因为增量将使逻辑或的任一侧都为真。

c logical-operators

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

如何调用绑定所有参数的 boost::function 对象

我一直在阅读and boost::function,但是,如果所有参数都被绑定,boost::bind我似乎无法找出调用函数的“好方法” (我认为这是正确的术语)。boost下面是未经测试的 MCVE(复制粘贴我的真实代码并不理想)。

#include "boost/function.hpp"
#include "boost/bind.hpp"
#include <iostream>

void func(void* args)
{
  int* myInt = static_cast<int*>(args);
  if (myInt != NULL)
  {
    std::cout << "myInt = " << *myInt << std::endl;
  }
  else
  {
    std::cout << "args is NULL" << std::endl;
  }
}

int main()
{
  int myInt1 = 5;
  int myInt2 = 45;

  boost::function<void(void* args)> myFunc = boost::bind(&func, &myInt1);

  // here's my problem,, how do I call myFunc?
  myFunc();  // this …
Run Code Online (Sandbox Code Playgroud)

c++ boost c++03

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

我的 C++ 继承代码中的编译器错误

我正在学习 C++ 中的继承。因此,每当创建类的对象时,都会调用构造函数。构造函数用于初始化类变量。

#include<bits/stdc++.h>
using namespace std;
class Base
{
    protected:
        int x;
    public:
        
        Base(int a): x(a)
        {
            cout<<"Base"<<endl;
        }
};
class Derived: public Base
{
    private:
        int y;
    public:
                                      
        Derived(int b):y(b)
        {
            cout<<"Derived"<<endl;
        }
    
        void print()
        {
            cout<<x<<" "<<y<<endl;
        }
};
int main()
{
    Derived d(20);
    d.print();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

从这里开始,我正在创建基类的对象并在其上调用打印函数。所以我应该得到输出。但是我的代码给出了编译器错误,为什么?任何人都可以帮助我理解这一点吗?

c++ inheritance class object

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