我想使用部分模板专门化来将数组(在编译时创建)“分解”为由其值组成的参数包(以与我在代码中定义的其他结构交互)。以下(我的第一次尝试)未编译
\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...}> {};\nRun Code Online (Sandbox Code Playgroud)\n\n因为模板实参std::array<long unsigned int, sizeof... (A)>{A ...} 一定不能涉及模板形参。据我了解,如果非类型参数非常依赖于模板参数,则不可能在部分模板专业化中提供非类型参数。因此,我尝试通过将值包含在类型中来解决这个问题:
#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}\nRun Code Online (Sandbox Code Playgroud)\n\n然而,在这里,当我尝试使用它时,部分模板专业化失败了;上面的定义与我使用它的方式不符,我不确定为什么。它失败并出现以下错误:
\n\nfile.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) 我正在处理一些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) 我正在尝试使用 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) 在 中C++17,void_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?
在哪些用例中使用[[nodiscard]]类型是有益的?
在类型上,[[nodiscard]]如果省略返回该类型实例的任何函数的返回值,则发出警告;(引自 p0068r0):
如果 [[nodiscard]] 被标记在一个类型上,它使得所有返回该类型的函数都隐式地 [[nodiscard]]。
而[[nodiscard]]构造函数(c++2a)对于管理资源的类(例如unique_ptr)和函数的 nodiscard非常有用,例如因为make_unique我无法提出一个类型的 nodiscard 有用的示例,我对使用的场合。
我想知道与下面绘制的代码类似的代码在 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出于某种原因优化调用可能是合法的(即,因为它丢弃了结果值,因为未定义元组中的执行顺序,并且它可以确定我们以任何一种方式丢弃第一个值?)
f = iter([0, 1, 2, 3, 4])
print(list(skip_first(f)))
Run Code Online (Sandbox Code Playgroud)
当 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 文档字符串中输入“与此元类关联的类”。有没有一个词可以更简洁地表示这种关系?
我想使用更简洁的命名法的浓缩示例:
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) 编译器是否保证评估环境constexpr中"tautologies"(例如始终true或false分别)的布尔表达式constexpr?
例如,在以下代码片段(在标有 的行(1))中,我在constexpr环境中调用了一个函数,我打算在non-constexpr传递函数时导致编译时错误。至少我使用的编译器 ( g++-10.0) 是这样做的,即使它也可以意识到表达式总是true不计算它。我问这个问题的原因是 - 据我所知 - 在非 constepxr 上下文中,像这样的表达式i >= std::numeric_limits<int>::min()被优化true为int 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++ ×4
python ×4
c++20 ×3
python-3.x ×3
c++17 ×2
attributes ×1
c++-concepts ×1
consteval ×1
constexpr ×1
enable-if ×1
inline ×1
jax ×1
jit ×1
lambda ×1
metaclass ×1
noexcept ×1
non-type ×1
onnxruntime ×1
pytorch ×1
sfinae ×1
templates ×1
terminology ×1
tuples ×1
void-t ×1