小编son*_*yao的帖子

假|| 在MinGW Compiler v 6.3.0-1中为true给出0

这是我写的一个C++程序:

#include <iostream>
using namespace std;

int main() {

    cout << "\n" << "false || false" << ": " << false || false;
    cout << "\n" << "false || true" <<  ": " << false || true;
    cout << "\n" << "true || false" << ": " <<  true || false;
    cout << "\n" << "true || true" << ": " <<  true || true;
    cout << "\n" << "false && false" << ": " << false && false;
    cout …
Run Code Online (Sandbox Code Playgroud)

c++ boolean mingw operator-precedence mingw32

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

为什么我们不能将"="用于shared_ptr或unique_ptr?

我知道语法得到编译错误.

std::shared_ptr<int> p = new int(5);

31 41 E:\temprory (delete it if u like)\1208.cpp [Error] conversion from 'int*' to non-scalar type 'std::shared_ptr<int>' requested
Run Code Online (Sandbox Code Playgroud)

但没关系

std::shared_ptr<int> p(new int(5));
Run Code Online (Sandbox Code Playgroud)

同样的unique_ptr;

但我不知道为什么禁止它.

c++ initialization shared-ptr unique-ptr c++11

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

如何使用布尔模板参数启用成员函数?

我希望一个类具有 , 的两种不同实现push,并根据布尔模板参数进行选择。我尝试使用本答案中描述的 SFINAE 原则,如下所示:

template<class T, bool foo=true>
class Bar {
  template <>
  typename std::enable_if<foo>::type
  push(const T& value) { /* one implementation */}

  template <>
  typename std::enable_if<!foo>::type
  push(const T& value) { /* another implementation */ } 
}
Run Code Online (Sandbox Code Playgroud)

但是,我push在 gcc 下收到“无法专门化类范围内的函数”的错误,我不明白为什么。尽管我的代码与链接答案中的代码不完全一样,但它似乎非常相似,我无法发现关键区别。

我还尝试使用与此答案中建议的语法类似的语法,但它也不起作用(错误是“不能重新声明类成员”):

  template <bool enable=foo>
  typename std::enable_if<enable>::type
  push(const T& value) { /* one implementation */}

  template <bool enable=!foo>
  typename std::enable_if<enable>::type
  push(const T& value) { /* another implementation */ } 
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c++ templates sfinae

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

如何在c ++中解释"const int*const&variable"

当一个将两个变量别名为

int a;
const int &b = a;
Run Code Online (Sandbox Code Playgroud)

这两个变量实际上是相同的,因此应用于变量的任何更改a也会应用于变量b.但是,当使用指针完成相同的技巧时,它似乎无法以相同的方式工作,如以下程序所示:

#include <iostream>
int main(void) {

    int *a = (int*) 0x1;
    const int *const &b = a;// Now b should be an alias to a.
    a = (int*) 0x2;// This should change b to 0x2.
    std::cout << b << "\n";// Outputs 0x1 instead of the expected value of 0x2.

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

现在变量a似乎不是变量的别名b,但为什么呢?

c++ pointers const reference

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

重载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
查看次数

共享指针不会增加use_count

我试图了解如何std::shared_ptr在C++中使用.但它很混乱,我不明白如何创建指向同一对象的多个共享指针.即使是文档和在线资料也不是很清晰.

以下是我编写的一小段代码,用于尝试和理解其std::shared_ptr行为:

#include <iostream>
#include <memory>
using namespace std;

class Node
{
public:
    int key;
    Node()
    {
        key = 0;
    }
    Node(int k)
    {
        key = k;
    }
};

int main()
{
    Node node = Node(10);

    shared_ptr<Node> ptr1((shared_ptr<Node>)&node);
    cout << "Use Count: " << ptr1.use_count() << endl;

    // shared_ptr<Node> ptr2=make_shared<Node>(node);//This doesn't increase use_count
    shared_ptr<Node> ptr2((shared_ptr<Node>)&node);
    cout << "Use Count: " << ptr2.use_count() << endl;

    if (ptr1 == ptr2)
        cout << "ptr1 & ptr2 point to the …
Run Code Online (Sandbox Code Playgroud)

c++ reference-counting shared-ptr make-shared c++11

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

为什么`int*Get()`被称为`const int&Get()`?

我有一个B有两个方法的类,其中一个返回指向成员变量的指针,另一个返回对该变量的const引用.

我试着打电话给那些方法.在调用期间,我将返回值存储到相应的返回类型.

我期待适当的返回类型最终会调用适当的方法,但我得到一个编译错误说:

error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
   const int& refval2 =  b.Get(); `

这是我的代码:

#include <iostream>
class B{
public:
  int* Get(){
    return &x_;
  }
  const int & Get() const{
    return x_;
  }

private:
  int x_ = 0;
};

int main(){
  B b;
  const int& refval2 =  b.Get(); 
  int* pval2 =  b.Get(); 
}
Run Code Online (Sandbox Code Playgroud)

c++ overload-resolution c++11

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

如何在C++中创建临时变量

我有一个函数返回对我的类"记录"的实例的引用.

record& get_record(int key) {
    return lookup(key);
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,它返回一个引用而不是值.现在我稍微修改一下.

record& get_record(int key) {
    if (valid(key))
        return lookup(key);
    else {
        record x;
        x.valid=false;
        return x; //Here I really want to return a temporary variable
                  // and not a reference to a local variable.      
    }
}
Run Code Online (Sandbox Code Playgroud)

返回对局部变量的引用不是一个好主意是否正确?以及如何以一种临时变量的方式返回x?

c++ reference temporary

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

在C++的编译时检查类型是否为std :: basic_string <T>

我在我的C++代码中将is_string定义如下:

#include <string>

template <typename T>
struct is_string
{
    static const bool value = false;
};

template <class T, class Traits, class Alloc>
struct is_string<std::basic_string<T, Traits, Alloc>>
{
    static const bool value = true;
};

int main()
{
    std::cout << is_string<std::string>::value << std::endl;
    std::cout << is_string<std::wstring>::value << std::endl;

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

对于std :: string和std :: wstring都是如此.

但我需要一个这样的谓词:

is_string<char, std::string>::value //to be true
is_string<char, std::wstring>::value //to be false
is_string<wchar_t, std::string>::value //to be false
is_string<wchar_t, std::wstring>::value //to be true
Run Code Online (Sandbox Code Playgroud)

有可能实现它吗?

c++ templates

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

如何在lambda中捕获此对象的变量?

我在SO上看到了许多答案,询问this通过引用进行捕获,但是我有一个不同的问题。如果要捕获this对象拥有的特定变量怎么办?

例如:

auto rel_pose = [this->_last_pose["main_pose"],&pose](Eigen::VectorXd pose1, Eigen::VectorXd pose2)
    {
        // Some code
        return pose;
    };
Run Code Online (Sandbox Code Playgroud)

我想this按值捕获特定的变量,并在我的lambda表达式中使用它。为什么这不可能呢?

c++ lambda c++14

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