小编son*_*yao的帖子

如何将模板函数声明为模板化嵌套类的朋友?

如何get在可以访问私有构造函数的封闭范围中创建函数outer<T>::inner<U>

template <typename T>
struct outer {
    template <typename U>
    class inner {
        inner() {}
    public:
        friend inner get(outer&) {
            return {};
        }
    };
};


int main() {
    outer<int> foo;
    outer<int>::inner<float> bar = get<float>(foo);
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试通过制作inner一个template <typename V, typename W> friend inner<V> get(outer<W>&);但却无法正常工作来宣布它.

c++ templates friend

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

假|| 在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
查看次数

将 const 添加到指针类型

假设我有一个类模板:

template <class T>
struct A
{
  T value;

  void foo(auto fun) { 
    fun(value);
    // ^^^^^^^ Pass value as a const object
  }
};
Run Code Online (Sandbox Code Playgroud)

我想常量添加到value,打电话时fun 这样只有接受功能 TT const&T const*可调用。我最初的方法是创建foo一个 const 成员函数,但是对于引用和指针成员来说这失败了,因为 const 成员函数可以修改它们(你只是不能对这些成员进行变基)。

我也尝试使用std::add_const并将std::as_const值传递给参数函数 ( fun) 但这会进行如下转换:

T = MyData*                  // say T is the type "Pointer to MyData"
add_const<T> = MyData *const // constness is added to the pointer, 
                             // i.e. it becomes constant …
Run Code Online (Sandbox Code Playgroud)

c++ templates constants const-correctness type-traits

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

C++ 是否保证此 enum 与 int 构造函数重载解析?

考虑这个示例程序:

#include <iostream>

typedef enum { A, B, C } MyEnum;

struct S
{
    S(int) { std::cout << "int" << std::endl; }
    S(MyEnum) { std::cout << "MyEnum" << std::endl; }
};

S f()
{
    return A;
}

int main()
{
    S const s = f();
}
Run Code Online (Sandbox Code Playgroud)

使用 clang 和 gcc 编译,生成一个可执行文件,在运行时打印“MyEnum”。C++ 标准保证这种行为吗?

c++ overload-resolution implicit-conversion

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

C++ 中的“using namespace::std”是什么

我正在阅读其他人的一些代码片段,发现一行:

using namespace::std;
Run Code Online (Sandbox Code Playgroud)

我怀疑它的目的是using namespace std;,但有一些拼写错误。但令我惊讶的是,编译器毫无怨言地接受了这段代码。我用以下方法构建:

$ g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0

$ /usr/bin/g++ ${SRC} -std=c++11 -pthread -Wall -Wno-deprecated -o ${OUT}
Run Code Online (Sandbox Code Playgroud)

我想知道为什么这段代码有效,它会产生什么效果?我怀疑这是一个不好的做法。

c++ namespaces using-directives

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