小编Eme*_*pon的帖子

为什么模板内部的类型检查更严格?

我正在阅读S. Meyers的Effective Modern C++,我找到了一些我无法理解的东西.

第8项解释了为什么nullptr应该优先于0NULL.赞成的主要论点nullptr是在超载决议中更安全的行为.在实践中,你可以避免指针和整数类型之间的意外混淆,但这不是我的问题.

要了解我的实际问题,请考虑以下代码,该代码基于本书中使用的示例:

#include <memory>

class MyClass {
    int a;
};

// dummy functions that take pointer types
int    f1(std::shared_ptr<MyClass> spw){return 1;};  
double f2(std::unique_ptr<MyClass> upw){return 1.0;};
bool   f3(MyClass* pw){return true;};

// template that calls a function with a pointer argument
template<typename FuncType,
         typename PtrType>
auto CallFun(FuncType func, PtrType ptr) -> decltype(func(ptr))
{
    return func(ptr);
}

int main()
{
    // passing null ptr in three different ways
    // they all …
Run Code Online (Sandbox Code Playgroud)

c++ templates types overloading c++11

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

结构化绑定取代std :: tie滥用

在阅读这篇关于c ++ 17最终特性的摘要时,我对结构化绑定(强调我的)部分感到有些惊讶:

结构化绑定

到目前为止,有一种已知的技巧滥用std :: tie直接将元组或对分配给不同的变量,而不必手动处理结果类型.这是一个hack,并且变量必须存在,现在你可以声明变量并在一行中初始化它们:

auto [a,b,c] = getvalues();

需要大括号,getvalues返回一个元组.提案中没有提到std :: pair,因此不清楚它是否适用于在某些插入方法中由STL返回的pair.

我假设他们提到了这种用法 std::tie

int a,b,c;
std::tie(a,b,c) = std::make_tuple(1,2,3);
Run Code Online (Sandbox Code Playgroud)

我认为这是一种推荐的做法.

有人可以解释为什么他们将上述例子称为黑客攻击吗?

c++ stdtuple c++17 structured-bindings

7
推荐指数
3
解决办法
2001
查看次数

定时指针存在吗?

在某些情况下,我希望有一些基于时间的智能指针,例如缓存一些庞大的对象,但如果不使用,会在一定时间后自动释放.当触摸(解除引用)指针时,重新开始倒计时,如果需要在计算期间"锁定"对象,则还可以停止倒计时.就像是:

timed_ptr<Type,30> p = new Type(); \\object is deleted after 30 seconds, and pointer set to a checkable 'null' state

...

p.stop_count_down();
// do something with the object, guaranteed it won't expire while we still need it. 
p.start_count_down();
Run Code Online (Sandbox Code Playgroud)

在boost或其他库中是否存在此类任何内容?

c++ smart-pointers c++11

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

为什么 std::vector 没有释放方法?

我发现自己处于这样一种情况,我希望有一个类似于unique_ptrs release()for的情况std::vector<>。例如:

std::vector<int> v(SOME_SIZE);

//.. performing operations on v

int* data = v.release(); // v.size() is now 0 and the ownership of the internal array is released
functionUsingAndInternallyDeletingRowPointer(data);
Run Code Online (Sandbox Code Playgroud)

有什么特殊原因不提供这种可能性吗?std::vector这会对内部实施施加一些限制吗?

或者有一种方法可以实现这一点,而我却尴尬地错过了?

c++ stl vector std allocator

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

Clang-Format:如何在 switch 语句中获取一行 case 语句

是否可以获得switch格式如下的语句:

switch (index) {
  case 0: /* statement */ break;
  case 1: /* statement */ break;
  default: break;
}
Run Code Online (Sandbox Code Playgroud)

使用 Clang 格式?

c++ clang-format

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

在一个捕获`this`的lambda中使用`this->`

有几个类似的问题,但我找不到这个特定点的明确答案.

它是否完全等同this->于在lambda中调用方法或成员变量时使用或不使用捕获this,或者存在一些细微差别?

class C {

    int var;
    void foo();

    void fool() {

       auto myLambda = [this] () {
           //
           this->var = 1;
           this->foo();
           // 100% equivalent to?
           var = 1;
           foo();
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

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

使用std :: shared_ptr的共享数据类

我需要一个实现共享数据语义的类,并且可能std::shared_ptr是一个很好的起点.我认为这种类的典型实现可以使用私有shared_ptr的共享数据,然后至少实现复制构造函数和operator=.

就像是:

class SharedDataClass {

public:
  SharedDataClass(const SharedDataClass& other)
  {
    data_ = other.data_;
  };

  SharedDataClass& operator=(const SharedDataClass& other)
  {
    data_ = other.data_;
    return *this;
  }

private:
  std::shared_ptr<DataType> data_;
};
Run Code Online (Sandbox Code Playgroud)

我想问一下是否有人批评上述实施.是否有任何其他成员/运营商应该实施以保持一致性?

c++ shared-ptr c++11

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

概念:要求类方法返回类型是同一类的嵌套类型

我正在尝试实现以下概念

template<typename T>
concept GameLogic = requires(T a)  {
    typename T::StateType;
    typename T::EventType;
    { a.initialState()->T::StateType }; // <-- relevant bit
};
Run Code Online (Sandbox Code Playgroud)

我想强制initialState()返回类型是同一类的嵌套类型。

概念定义不会引发错误(gcc 9.2),但以下实现GameLogic无法满足要求:

class SimpleGameLogic {
public:

    using StateType = SimpleState;
    using EventType = SimpleEvent;

    StateType initialState() {
        return _initialState;
    }

private:
    StateType _initialState;

};
Run Code Online (Sandbox Code Playgroud)

我已经尝试了上述语法的一些变体,但找不到正确的语法...或者这可能尚未实现?我究竟做错了什么?

c++ c++-concepts c++20

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