小编mut*_*oid的帖子

如何将编译时 std::array“扩展”为参数包?

我想使用部分模板专门化来将数组(在编译时创建)“分解”为由其值组成的参数包(以与我在代码中定义的其他结构交互)。以下(我的第一次尝试)未编译

\n\n
#include <array>\n\ntemplate <typename T, auto k> struct K;\ntemplate <typename T, std::size_t... A> struct K<T, std::array<std::size_t, sizeof...(A)>{A...}> {};\n
Run Code Online (Sandbox Code Playgroud)\n\n

因为模板实参std::array<long unsigned int, sizeof... (A)>{A ...} 一定不能涉及模板形参。据我了解,如果非类型参数非常依赖于模板参数,则不可能在部分模板专业化中提供非类型参数。因此,我尝试通过将值包含在类型中来解决这个问题:

\n\n
#include <array>\n\ntemplate <auto f> struct any_type;\n\ntemplate <typename T, typename array_wrapper> struct FromArr;\ntemplate <typename T, std::size_t... A>\nstruct FromArr<T, any_type<std::array<std::size_t, sizeof...(A)>{A...}>> {};\n\nint main() {\n  FromArr<int, any_type<std::array<std::size_t, 2>{1, 2}>> d;\n  (void) d;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然而,在这里,当我尝试使用它时,部分模板专业化失败了;上面的定义与我使用它的方式不符,我不确定为什么。它失败并出现以下错误:

\n\n
file.cc: In function \xe2\x80\x98int main()\xe2\x80\x99:\nfile.cc:10:55: error: aggregate \xe2\x80\x98FromArr<int, Any<std::array<long unsigned int, 2>{std::__array_traits<long unsigned int, 2>::_Type{1, 2}}> > d\xe2\x80\x99 …
Run Code Online (Sandbox Code Playgroud)

c++ templates non-type c++20

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

`inline` 和 `noexcept` 在 consteval 上下文中是多余的吗?

我正在处理一些constexpr使用函数的代码,我目前consteval尽可能将其重构为。

inline constexpr auto example() noexcept { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

据我了解inline上面的关键字在constexpr函数中已经是多余的了。

据我所知,noexcept关键字对于consteval函数来说是多余的,因为据我所知,它consteval必须在编译时进行评估,因此意味着 noexcept。这是真的还是我目前不考虑的东西(比如 constexpr exceptions)?

consteval auto example() { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

c++ inline noexcept c++20 consteval

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

Onnxruntime:仅当首先导入 pytorch 时,才能在 GPU 上使用 CUDNN 进行推理

我正在尝试使用 onnxruntime-gpu 进行推理。因此,我在我的系统上安装了 CUDA、CUDNN 和 onnxruntime-gpu,并检查我的 GPU 是否兼容(下面列出的版本)。

当我尝试启动推理会话时,我收到以下警告:

>>> import onnxruntime as rt
>>> rt.get_available_providers()
['TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider']
>>> rt.InferenceSession("[ PATH TO MODEL .onnx]", providers= ['CUDAExecutionProvider'])
2023-01-31 09:07:03.289984495 [W:onnxruntime:Default, onnxruntime_pybind_state.cc:578 CreateExecutionProviderInstance] Failed to create CUDAExecutionProvider. Please reference https://onnxruntime.ai/docs/reference/execution-providers/CUDA-ExecutionProvider.html#requirements to ensure all dependencies are met.
<onnxruntime.capi.onnxruntime_inference_collection.InferenceSession object at 0x7f740b4af100>
Run Code Online (Sandbox Code Playgroud)

但是,如果我先导入 torch,推理就会在我的 GPU 上运行,并且一旦开始推理会话,我就会看到我的 python 程序列在 nvidia-smi 下:

$ python
Python 3.8.16 (default, Dec  7 2022, 01:12:06)
[GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more …
Run Code Online (Sandbox Code Playgroud)

python pytorch onnxruntime

5
推荐指数
0
解决办法
3121
查看次数

结合void_t和enable_if?

在 中C++17void_t允许使用class/struct模板轻松执行 SFINAE:

template <class T, class = void>
struct test {
    static constexpr auto text = "general case";
};

template <class T>
struct test<T, std::void_t<decltype(std::begin(std::declval<T>())>> {
    static constexpr auto text = "has begin iterator";
};
Run Code Online (Sandbox Code Playgroud)

里面的东西void_t是一个类型。void_t我的问题是:当里面的内容是类型特征时,如何做同样的事情。使用enable_if效果很好:

template <class T>
struct test<T, std::void_t<std::enable_if_t<std::is_class_v<T>>> {
    static constexpr auto text = "is a class";
};
Run Code Online (Sandbox Code Playgroud)

是否有更短/更优雅的方式来写这个,或者“正确的方式”来做到这一点,真的是结合void_tand enable_if

sfinae enable-if template-meta-programming c++17 void-t

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

拥有 [[nodiscard]] 类型的理由是什么?

在哪些用例中使用[[nodiscard]]类型是有益的?

在类型上,[[nodiscard]]如果省略返回该类型实例的任何函数的返回值,则发出警告;(引自 p0068r0):

如果 [[nodiscard]] 被标记在一个类型上,它使得所有返回该类型的函数都隐式地 [[nodiscard]]。

[[nodiscard]]构造函数(c++2a)对于管理资源的类(例如unique_ptr)和函数的 nodiscard非常有用,例如因为make_unique我无法提出一个类型的 nodiscard 有用的示例,我对使用的场合。

c++ attributes c++17

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

Python:生成元组的语句的执行顺序是否有保证?多语句 lambda

我想知道与下面绘制的代码类似的代码在 Python 中是否合法并且可以安全使用:

# Example usage of a lambda which executes a statement and returns the changed operand
skip_first = lambda iterator: (next(iterator), iterator)[1]
Run Code Online (Sandbox Code Playgroud)

使用这样的构造并期望 lambda 的返回值是一个从原始迭代器的第二个元素开始的迭代器(如果没有抛出异常)是否安全?

或者 fi Cythonnext出于某种原因优化调用可能是合法的(即,因为它丢弃了结果值,因为未定义元组中的执行顺序,并且它可以确定我们以任何一种方式丢弃第一个值?)

  1. 用法示例:
f = iter([0, 1, 2, 3, 4])
print(list(skip_first(f)))
Run Code Online (Sandbox Code Playgroud)
  1. Godbolt 的例子

python lambda tuples python-3.x

3
推荐指数
1
解决办法
55
查看次数

JAX:避免对沿一个轴使用不同数量的元素进行评估的函数进行即时重新编译

当 JIT 函数的输入结构基本保持不变(除了一个轴具有不同数量的元素之外)时,是否可以避免重新编译 JIT 函数?

import jax

@jax.jit
def f(x):
    print('recompiling')
    return (x + 10) * 100

a = f(jax.numpy.arange(300000000).reshape((-1, 2, 2)).block_until_ready()) # recompiling
b = f(jax.numpy.arange(300000000).reshape((-1, 2, 2)).block_until_ready())
c = f(jax.numpy.arange(450000000).reshape((-1, 2, 2)).block_until_ready()) # recompiling. It would be nice if it weren't
Run Code Online (Sandbox Code Playgroud)

要求:pip install jax、jaxlib

python jit python-3.x jax

3
推荐指数
1
解决办法
1069
查看次数

类与其关联元类的关系的命名法是什么?

我们将类与其关联元类的关系称为什么?

我想在以下两行中填空:

  • A类是B类的元类。
  • B类是A类的____。

在该类的文档中,我正在记录我目前正在编写的元类。我发现自己经常在 python 文档字符串中输入“与此元类关联的类”。有没有一个词可以更简洁地表示这种关系?

我想使用更简洁的命名法的浓缩示例:

def __init__(mcl, what, bases=None, dict=None):
  """
  Raises an exception if >> the class associated to this metaclass << 
  contains a valid set of configuration decorators.
  ...
  """
Run Code Online (Sandbox Code Playgroud)

python terminology metaclass python-3.x

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

编译时评估代码中的重言式是否保证被执行/优化掉?

编译器是否保证评估环境constexpr"tautologies"(例如始终truefalse分别)的布尔表达式constexpr

最小示例/说明

例如,在以下代码片段(在标有 的行(1))中,我在constexpr环境中调用了一个函数,我打算在non-constexpr传递函数时导致编译时错误。至少我使用的编译器 ( g++-10.0) 是这样做的,即使它也可以意识到表达式总是true不计算它。我问这个问题的原因是 - 据我所知 - 在非 constepxr 上下文中,像这样的表达式i >= std::numeric_limits<int>::min()被优化trueint i.

#include <limits>
constexpr int example_function() { return 1;}
constexpr bool compileTimeErrorDesired = example_function() || true; // (1)
Run Code Online (Sandbox Code Playgroud)

应用实例

如果(1) 保证in 的行为,则可以在 aconcept中使用它,以执行不同的代码,具体取决于是否可以在编译时评估作为模板参数提供的函数。我实现了一个非常短的 ( 7 lines-of-code) 示例,它在编译器资源管理器中完全做到这一点。

如果使用非 constexpr 函数调用,行 (1) …

c++ language-lawyer constexpr c++-concepts c++20

0
推荐指数
1
解决办法
129
查看次数