目前,我正在学习 C++,并决定从 C++20 开始。然而,这些代码让我发疯,因为我认为结果没有任何意义。
以下代码将Valid array.打印句子。我上面的意思是,这是不对的。它根本不应该打印句子,因为我在参数中插入的类型与概念不匹配。
在 VS2022 Preview 3 和具有最新 GCC 和 C++2A(GNU) 参数的在线编译器上进行测试,生成了相同的结果。
#include <array>
#include <cstdio>
#include <iostream>
#include <type_traits>
using namespace std;
template<typename A> concept ProperArray = requires(A array)
{
{array.max_size() >= 2U};
{std::is_arithmetic_v<typename A::value_type> == true};
};
int main()
{
constexpr bool b = ProperArray<array<std::string, 1>>;
if constexpr (b)
cout << "Valid array." << endl;
std::cout << "Hello, Wandbox!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 自从std::generator正在进入 CPP23,我正在尝试 MSVC 的不完整版本。
然而,我注意到,yield与 一起使用时,它似乎恰好丢失了一个std::views::take。这是示例:
#include <iostream>
#include <ranges>
#include <experimental/generator>
std::experimental::generator<int> GeneratorFn(void) noexcept
{
co_yield 1;
co_yield 2;
co_yield 3;
co_yield 4;
co_yield 5;
co_yield 6;
co_yield 7;
co_yield 8;
co_yield 9;
co_return;
}
int main(int argc, char** args) noexcept
{
auto Ret = GeneratorFn();
for (auto&& i : Ret | std::views::take(2))
std::cout << i << '\n';
for (auto&& i : Ret | std::views::take(3))
std::cout << i << '\n';
for (auto&& i …Run Code Online (Sandbox Code Playgroud) 最近我一直在尝试为一个老游戏制作一个插件,并遇到了类似于钻石继承的问题。
我有一个非常精简的例子,写如下:
#include <iostream>
#include <stdint.h>
#include <stddef.h>
using namespace std;
struct CBaseEntity
{
virtual void Spawn() = 0;
virtual void Think() = 0;
int64_t m_ivar{};
};
struct CBaseWeapon : virtual public CBaseEntity
{
virtual void ItemPostFrame() = 0;
double m_flvar{};
};
struct Prefab : virtual public CBaseEntity
{
void Spawn() override { cout << "Prefab::Spawn\n"; }
void Think() override { cout << "Prefab::Think\n"; }
};
struct WeaponPrefab : virtual public CBaseWeapon, virtual public Prefab
{
void Spawn() override { …Run Code Online (Sandbox Code Playgroud)