我的类定义分布在头文件和源文件中:
// T.hpp
class T {
public:
void foo();
};
// T.cpp
void T::foo() {
}
Run Code Online (Sandbox Code Playgroud)
如果T::foo需要使用一些只T需要可见的辅助函数,以下哪种解决方案最好?
// 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)
// T.hpp
class T {
public:
void foo();
};
// T.cpp
namespace {
void helper() {}
}
void T::foo() {
helper();
}
Run Code Online (Sandbox Code Playgroud)
有没有区别,除了前者我会在头文件中有更多的功能?
我有一个问题,理解为什么shared_ptr使用原子CPU指令...我无法弄清楚原因,因为它不是线程安全的.有人可以解释一下.
如果你想知道我是如何知道它使用原子intstuructions:有一个来自C++的剪辑,除了Herb和Andrei谈论它之外,但他们从未提及为什么会这样.
当一个互斥锁已被T1锁定,并且T2试图锁定它时,T2的进程是什么?
我认为它是这样的:
-T2试图锁定,失败,也许是自旋锁,然后调用yield ...
-T2计划执行几次,尝试锁定失败,产生...... -
最终T1解锁,T2计划执行,设法锁定互斥锁......
T1解锁是否明确地向调度程序或其他线程发出信号,表示互斥锁已解锁?或者它只是解锁,并让调度程序在感觉适合时调度被阻塞的线程(也称调度程序没有阻塞线程的概念并且不将它们视为特殊)?
我试着寻找一种可以做std::inplace_merge
后续std::unique做法的算法.在1遍中比在2中更有效率.无法在标准库中找到它或通过oogling找到它.
目前我能想到的最好的方法是使用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) 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) 有时我想做
bool success= true;
success &&= dosmthing1();
success &&= dosmthing2();
success &&= dosmthing3();
if (success)
Run Code Online (Sandbox Code Playgroud)
让我们忽略我可以使用异常...我的问题是它是否由C++标准保证,对我的用例来说&=就像不存在&&=一样?...
编辑:做smthing-s返回布尔
据我所知,部分纸,他们欺骗了CPU到内存受害者的部分推测加载到CPU高速缓存.第一部分我不明白他们是如何从缓存中检索它的.
我实现了一个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
我正在学习概念,我想不出一种方法来限制非类型模板参数的值(不是类型)。
编译的代码示例,尽管我希望它没有(由于需求失败):
#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++ ×9
c++20 ×3
c++-concepts ×2
atomic ×1
c ×1
c++11 ×1
c++17 ×1
copy-elision ×1
cpu ×1
mutex ×1
non-type-template-parameter ×1
rvo ×1
security ×1
shared-ptr ×1
spectre ×1
stdtuple ×1
stl ×1