小编lew*_*wis的帖子

如何做简单的 C++ 概念 has_eq - 与 std::pair 一起使用(对于 C++20,std::pair 运算符 == 已损坏)

编译器资源管理器链接

\n
template <typename T>\nconcept HasEq = requires(T t) {\n    { t == t } -> std::convertible_to<bool>;\n};\n\nstruct X {};\nstatic_assert(not HasEq<X>);\n//bool a = pair<X, X>{} == pair<X, X>{};\nstatic_assert(! HasEq<pair<X, X>>);  // fails! SIGH\n
Run Code Online (Sandbox Code Playgroud)\n

我想它\xe2\x80\x99 足够简单,可以定义一个概念“T 支持==”。它\xe2\x80\x99很简单,可以定义一个不支持运算符==的类型\'X\'。这个概念似乎对此很有效。

\n

但令人困惑的是,pair<X,X> 并不真正支持运算符==(因为它委托给不存在的X 运算符==)。

\n

然而 HasEq<pair<X, X>> 返回错误的答案(它表示已经定义了operator==)。

\n

似乎是 std C++ 运算符 ==(pair,pair) 定义的错误,无条件定义运算符 ==,而不是在运算符 == 定义上使用 \'enable_if\' 或 \'requires\'。但我不太确定我能做些什么来使 HasEq 正常工作(所以首先要了解这是否确实是 st​​d::pair 运算符== 定义中的缺陷)。

\n

c++ concept

19
推荐指数
2
解决办法
1425
查看次数

编译和评估 conditional_t 时出现错误情况的问题

我很难理解如何阻止代码std::conditional_t在错误分支中被评估。

\n
#include <type_traits>\n\nusing namespace std;\n\nnamespace {\n    template <typename T>\n    using BaseDifferenceType = decltype (T{} - T{});\n}\n\nint main ()\n{\n    using T = int;\n    static_assert(! is_enum_v<T>);\n    BaseDifferenceType<T> xxx {};\n    \n    // PROBLEM IS HERE - though is_enum is false, we still evaluate underlying_type<T> and complain it fails\n    using aaa = conditional_t<is_enum_v<T>, underlying_type_t<T>, BaseDifferenceType<T>>;\n    \n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

您可以在https://www.onlinegdb.com/uxlpSWVXr在线尝试此操作。

\n

编译(使用 C++17)给出错误:

\n
#include <type_traits>\n\nusing namespace std;\n\nnamespace {\n    template <typename T>\n    using BaseDifferenceType = decltype (T{} - T{});\n}\n\nint …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++17 conditional-types

7
推荐指数
2
解决办法
661
查看次数

macos ${{ env.TMPDIR }} 在 github 操作中不起作用

我有一个 github 操作尝试使用 ${{ env.TMPDIR }} 并且似乎没有扩展到 TMPDIR 环境变量。

 - name: DEBUG Workaround GitHub-Actions-MacOS Issue with env.TMPDIR
    run: |
      echo "TMPDIR=$TMPDIR"
      echo "TMPDIR using ENV.TMPDIR=${{ env.TMPDIR }}"
Run Code Online (Sandbox Code Playgroud)

这会产生:

TMPDIR=/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/
TMPDIR using ENV.TMPDIR=
Run Code Online (Sandbox Code Playgroud)

示例: 使用 env.TMPDIR 调试解决方案 GitHub-Actions-MacOS 问题

macos github-actions

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

如何使github动作矩阵元素有条件

我有一个使用 'strategy' = 'matrix' 和要构建的特定配置列表的工作流。

链接到工作流 YAML(也在下面提供)

#
# build-N-test-v2.1-Dev and build-N-test-v2.1-Release are neary
# identical, but a few tests are commented out (to not needlessly stress CI system)
# for v2.1-Dev builds
#
# NOTE: I've tried many tricks - none which seem to work - to get this working on one file with one
# workflow and tests
#     https://github.community/t/what-is-the-correct-if-condition-syntax-for-checking-matrix-os-version/16221
#     https://github.community/t/how-to-conditionally-include-exclude-items-in-matrix-eg-based-on-branch/16853
#
# but none seem to work
#

name: build-N-test-v2.1-Dev

on:
  push:
    branches:
      - v2.1-Dev
      #- …
Run Code Online (Sandbox Code Playgroud)

github github-actions building-github-actions

5
推荐指数
4
解决办法
3441
查看次数

如何在 C++ 中声明和初始化字节数组

这真的是声明字节(或字节数组)的最佳方式吗?

static constexpr byte kGuard1_[] = {
            byte{0x45}, byte{0x23}, byte{0x12}, byte{0x56}, byte{0x99}, byte{0x76}, byte{0x12}, byte{0x55},
        };
Run Code Online (Sandbox Code Playgroud)

为什么没有一些后缀(如 b)可以用来直接将数字标记为字节?或者问题只是因为我使用了统一初始化?

c++ byte literals uniform-initialization c++20

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

decltype、dyn、impl 特征以及重构时如何声明函数的返回类型

我是 Rust 新手,但是是从 C++ 角度/背景接触 Rust 的编程老手,所以这可以解释我的大部分困惑。

我的核心是一个需要解决的简单得令人麻木的问题。然而,生锈中看似最基本的东西却让我陷入了麻烦的老鼠窝。

我有一些代码:

pub fn my_application(cfg: &mut web::ServiceConfig) {
   let auth = HttpAuthentication::bearer(validate_bearer);
   ...
Run Code Online (Sandbox Code Playgroud)

我想重构它。我想使用我自己的自定义中间件,而不是使用“actix”HttpAuthentication::bearer 中间件。

所以我想做就是采用“let auth = ...”行并将等号后面的内容放入另一个文件中的函数中。

那有多难?

所以我尝试了显而易见的方法:在一个新文件中:

// WONT repeat 'use' statements in rest, but included once in case you try to compile)
use actix_web::dev::ServiceRequest;
use actix_web::Error;
use actix_web_httpauth::extractors::bearer::BearerAuth;
use actix_web_httpauth::middleware::HttpAuthentication;
use core::future::Future;
use crate::server::auth::common::validate_bearer;

// in C++, you would might say auto, or some such
pub fn create_auth_middleware_1()
{
    let auth = HttpAuthentication::bearer(validate_bearer);
    auth
}
Run Code Online (Sandbox Code Playgroud)

那失败了: …

decltype rust actix-web

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

验证发行者 - 令牌具有发行者 https://login.microsoftonline.com/Xv2.0 但示例暗示我应该使用 https://sts.windows.net/X 进行验证

我正在尝试遵循https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapi-manual-jwt-validation/ 中的示例验证代码

(真的是https://github.com/Azure-Samples/active-directory-dotnet-webapi-manual-jwt-validation/blob/master/TodoListService-ManualJwt/Global.asax.cs#L136 中的代码)

然后我尝试使用以下方法进行验证:

TokenValidationParameters validationParams = new TokenValidationParameters
{
    // We accept both the App Id URI and the AppId of this service application
    ValidAudiences = new[] { kADConfiguration_.Audience, kADConfiguration_.ClientId },

    // Supports both the Azure AD V1 and V2 endpoint
    ValidIssuers = new[] { …
Run Code Online (Sandbox Code Playgroud)

azure azure-active-directory

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