小编NoS*_*tAl的帖子

我应该在哪里实现这个"私人"帮助函数?

我的类定义分布在头文件和源文件中:

// T.hpp

class T {
   public:
      void foo();
};

// T.cpp

void T::foo() {

}
Run Code Online (Sandbox Code Playgroud)

如果T::foo需要使用一些T需要可见的辅助函数,以下哪种解决方案最好?

1.私人会员

// T.hpp

class T {
   public:
      void foo();
   private:
      void helper();
};

// T.cpp

void T::foo() {
    helper();
}

void T::helper() {
}
Run Code Online (Sandbox Code Playgroud)

2.仅在类定义的TU中可访问的自由函数

// T.hpp

class T {
   public:
      void foo();
};

// T.cpp

namespace {
    void helper() {}
}

void T::foo() {
    helper();
}
Run Code Online (Sandbox Code Playgroud)

有没有区别,除了前者我会在头文件中有更多的功能?

c++

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

为什么std :: shared_ptr使用原子cpu操作

我有一个问题,理解为什么shared_ptr使用原子CPU指令...我无法弄清楚原因,因为它不是线程安全的.有人可以解释一下.

如果你想知道我是如何知道它使用原子intstuructions:有一个来自C++的剪辑,除了Herb和Andrei谈论它之外,但他们从未提及为什么会这样.

c++ atomic shared-ptr c++11

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

互斥实现和信令

当一个互斥锁已被T1锁定,并且T2试图锁定它时,T2的进程是什么?

我认为它是这样的:

-T2试图锁定,失败,也许是自旋锁,然后调用yield ...
-T2计划执行几次,尝试锁定失败,产生...... -
最终T1解锁,T2计划执行,设法锁定互斥锁......

T1解锁是否明确地向调度程序或其他线程发出信号,表示互斥锁已解锁?或者它只是解锁,并让调度程序在感觉适合时调度被阻塞的线程(也称调度程序没有阻塞线程的概念并且不将它们视为特殊)?

c c++ implementation mutex

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

为什么没有std :: inplace_merge_unique?

我试着寻找一种可以做std::inplace_merge 后续std::unique做法的算法.在1遍中比在2中更有效率.无法在标准库中找到它或通过oogling找到它.

  1. 那么在不同的名称下可能会在某处提升实现吗?
  2. 这种算法是否可行(从某种意义上说,它具有与普通inplace_merge相同的复杂性保证)?

c++ stl

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

C++ Concepts是否允许我的类在声明/定义中指定它满足某些概念?

目前我能想到的最好的方法是使用static_assert,但我更喜欢更好的方式.

#include <set>
#include <forward_list>

using namespace std;

template<typename C>
concept bool SizedContainer = requires (C c){
    c.begin();
    c.end();
    {c.size()} -> size_t;
};

static_assert(SizedContainer<std::set<int>>);
static_assert(!SizedContainer<std::forward_list<int>>);
static_assert(!SizedContainer<float>);

class MyContainer{
public:
    void begin(){};
    void end(){};
    size_t size(){return 42;}; 
};

static_assert(SizedContainer<MyContainer>);



int main()
{
}
Run Code Online (Sandbox Code Playgroud)

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

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

Why do I not get guaranteed copy elision with std::tuple?

I would expect that in C++20 the following code prints nothing between prints of A and B (since I expect guaranteed RVO to kick in). But output is:

A

Bye

B

C

Bye

Bye

So presumably one temporary is being created.

#include <iostream>
#include <tuple>
struct INeedElision{
    int i;
    ~INeedElision(){
        std::cout << "Bye\n";
    }
};

std::tuple<int, INeedElision> f(){
    int i = 47;
    return {i, {47}};
}

INeedElision g(){
    return {};
}

int main()
{   
    std::cout << "A\n"; 
    auto x = …
Run Code Online (Sandbox Code Playgroud)

c++ copy-elision rvo stdtuple c++20

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

是否保证表现得像假设的&& =运算符?

有时我想做

bool success= true;
success &&= dosmthing1();
success &&= dosmthing2();
success &&= dosmthing3();
if (success)
Run Code Online (Sandbox Code Playgroud)

让我们忽略我可以使用异常...我的问题是它是否由C++标准保证,对我的用例来说&=就像不存在&&=一样?...

编辑:做smthing-s返回布尔

c++ logical-operators

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

Specter攻击如何读取欺骗CPU加载的缓存?

据我所知,部分,他们欺骗了CPU到内存受害者的部分推测加载到CPU高速缓存.第一部分我不明白他们是如何从缓存中检索它的.

security cpu cpu-architecture spectre side-channel-attacks

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

是否可以static_assert确定lambda不是通用的?

我实现了一个Visit函数(在变体上),该函数检查变体中当前活动的类型是否与函数签名(更确切地说是第一个参数)匹配。基于这个不错的答案。例如

#include <variant>
#include <string>
#include <iostream>

template<typename Ret, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(*) (Arg, Rest...));

template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...));

template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...) const);

template <typename F>
decltype(first_argument_helper(&F::operator())) first_argument_helper(F);

template <typename T>
using first_argument = decltype(first_argument_helper(std::declval<T>()));

std::variant<int, std::string> data="abc";
template <typename V>
void Visit(V v){
using Arg1 = typename std::remove_const_t<std::remove_reference_t<first_argument<V>>>;//... TMP magic to get 1st argument of visitor + remove …
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming variadic-templates generic-lambda c++17

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

非类型模板参数和需要

我正在学习概念,我想不出一种方法来限制非类型模板参数的值(不是类型)。

编译的代码示例,尽管我希望它没有(由于需求失败):

#include <cassert>

enum Bla{
    Lol, 
    Haha
};

template<Bla b>
requires requires{
    // my guess is that this just checks that this is valid expression, not
    // that it is true
    b>1; 
}
void f(){
    assert(b>1);
}

int main() {
    f<Lol>(); // compiles, not funny ;)
}
Run Code Online (Sandbox Code Playgroud)

注意:这是一个简化的例子(我想要“模板重载”)所以static_assert对我不利,我试图避免,std::enable_if因为语法是可怕的。

c++ c++-concepts c++20 non-type-template-parameter

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