出现以下问题:
c ++标准似乎在说,这std::vector
需要一个完整的类型才能起作用。(请参阅https://en.cppreference.com/w/cpp/container/vector)那么,为什么下面的代码仍然可以编译?
#include <vector>
struct parent;
struct child
{
std::vector<parent> parents; //parent is incomplete here!
};
struct parent
{
std::vector<child> children;
};
Run Code Online (Sandbox Code Playgroud)
这似乎违反直觉。如果std::vector
需要完整的类型,则std::vector<parent>
不应编译,因为在的类定义中仅知道其前向声明child
。
std::vector
了吗,不需要完整的类型?编辑
c ++ 11和c ++ 17之间似乎有所不同。我想了解c ++ 11版本。
我正在尝试一些事情并提出以下问题:是否有可能在 a 中存储对值的引用std::any
?
我尝试了以下方法:
#include <any>
#include <iostream>
#include <functional>
auto func_by_pointer(std::any obj)
{
*std::any_cast<int *>(obj) += 2;
}
auto modify_by_pointer(int &a)
{
func_by_pointer(std::make_any<int *>(&a));
}
auto func_by_reference_wrapper(std::any obj)
{
std::any_cast<std::reference_wrapper<int>>(obj).get() -= 2;
}
auto modify_by_reference_wrapper(int &a)
{
func_by_reference_wrapper(std::make_any<std::reference_wrapper<int>>(a));
}
auto func_by_reference(std::any obj)
{
std::any_cast<int &>(obj) *= 2;
}
auto modify_by_reference(int &a)
{
func_by_reference(std::make_any<int &>(a));
}
int main()
{
auto value = 3;
std::cout << value << '\n';
modify_by_pointer(value);
std::cout << value << '\n';
modify_by_reference_wrapper(value);
std::cout << value …
Run Code Online (Sandbox Code Playgroud) 我有一堆这样的结构,其中成员数量不断增加,但成员命名却保持一致:
struct one { int a; };
struct two { int a; int b; };
struct three { int a; int b; int c; };
Run Code Online (Sandbox Code Playgroud)
我也有一个模板化函数,我希望接受以下这些结构的成员之一:
template <typename T, typename ... ARGS> // T will be one, two, or three
void func(ARGS... args); // This should take 1, 2, or 3, int arguments respectively
Run Code Online (Sandbox Code Playgroud)
我希望能够这样称呼:
two foo;
func<two>(splatter(foo));
Run Code Online (Sandbox Code Playgroud)
哪里splatter
会以某种方式分裂,foo
以便解决func<two>(foo.a, foo.b)
。
显然,我可以只扩展此内联,而无需splatter
,但是我调用的代码func
本身很容易被模板化。我已经尝试过使用,initializer_list
但是我不知道如何仅基于模板类型来构建一个。
不幸的是,我的编译器也不支持constexpr if
对splat的调用func
或构建initializer_list …
我目前正在尝试研究 monad。不幸的是,大多数关于该主题的文章都使用 Haskell,但没有正确解释符号。然而,由于我主要用 C++ 编程,所以我想在不学习新的编程语言的情况下理解 monad...
根据我在网上收集的信息, monadM
是 type 的类型构造函数T
,它至少提供以下操作:
T
T
显然称为return )T
到函数的组合器(在 Haskell 中f
显然称为“bind” )将这些标准应用于 C++,在我看来,std::unique_ptr
可以将其视为一个 monad。这是真的?
我的推理如下:
模板std::unique_ptr
用于构造实际类型std::unique_ptr<T>
,因此:
std::unique_ptr<T>{}
或者std::make_unique<T>()
std::make_unique
(带有参数......)std::bind(func, pointer.get())
,std::bind(func, *pointer)
或等效的 lambdaoperator*()
您是否同意,或者对组合器的调用/.get()
对组合器的调用是否取消std::unique_ptr
了作为 monad 的资格?
我知道,用作std::unique_ptr
monad 可能没有意义,因为它带有所有者语义。我只是想知道,如果是的话。
我最近转向了 Linux,并尝试了几个开源项目。我尝试过的应用程序之一是用于 RAW 照片编辑的 GTK3 应用程序。然而,虽然一般计算速度很快,但它存在显着的输入滞后。单击、拖动或悬停时,GUI 需要相当长的时间才能做出响应。由于它是开源的,我想花一些时间尝试弄清楚是否可以解决这个问题。然而,我却卡在了第一步。我不知道如何获取程序的帧分析数据。
因此,我的问题是:如何获取此应用程序的分析数据?我正在考虑使用https://optick.dev/但是由于 GTK 是基于事件的,这似乎不是正确的选择。
注意: 我需要一个适用于 Linux 和 GTK3 的解决方案。不过,我会将解决方案授予解释大多数系统(Linux、Mac、Windows、?)和 GTK 版本的分析的答案。
我目前正在编写一个复杂的类,在其中我基本上需要复制派生类的列表。简化版本如下:我有一个基类,从中派生出几个其他类:
class Base
{
public:
virtual void test(void)
{
cout << "Base" << endl;
}
Base(vector<Base*> *pointer)
{
pointer->push_back(this);
}
virtual Base& operator=(const Base& rhs)
{
cout << "Base=" << endl;
return *this;
}
};
class A : public Base
{
public:
void test(void)
{
cout << "A" << endl;
}
A(vector<Base*> *pointer) : Base(pointer) {}
A& operator=(const A& rhs)
{
cout << "A=" << endl;
return *this;
}
};
class B : public Base
{
public:
void test(void)
{ …
Run Code Online (Sandbox Code Playgroud) c++ list operator-overloading derived-class assignment-operator
我现在已经完成了我的一个小图书馆。当我开始使用它时,我不知道 clang-format。现在我想用它格式化整个存储库。我知道随着提交哈希值的变化,这会破坏其他人的存储库。但是,由于还没有人在使用我的库,所以这对我来说没问题。
因此,我必须做什么才能为我的历史记录中的每个提交运行 clang-format?