我有一个带有int模板参数的类.在某些情况下,我希望它输出一条错误消息.此消息应该是来自某些固定文本和模板参数的串联字符串.出于性能原因,我希望每次发生错误时都避免在运行时构建此字符串,理论上两者都是,字符串文字和模板参数在编译时是已知的.所以我正在寻找将其声明为constexpr的可能性.
代码示例:
template<int size>
class MyClass
{
void onError()
{
// obviously won't work but expressing the concatenation like
// it would be done with a std::string for clarification
constexpr char errMsg[] = "Error in MyClass of size " + std::to_string (size) + ": Detailed error description\n";
outputErrorMessage (errMsg);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个私有 GitHub 存储库 A,它通过 GitHub Actions 构建一个库,并在构建工作流程结束时将构建的库作为工件上传。由于许可证原因,包含构建库的此存储库必须保持私有。
另一个公共存储库 B 现在应该在其基于 GitHub Actions 的构建工作流程期间链接到该库。我只知道可用于管理工作流程中操作的常用操作upload-artifact和download-artifact操作,但这似乎不是解决我的问题的方法。
是否有任何官方方法可以实现我想要的目标,是否有任何技巧可以用来使其发挥作用,或者这根本不可能?如果是这样,您有替代方法的建议吗?
我正在尝试Kokkos 参考 mdspan 实现,看看它是否可以用来简化我们代码库中的一些代码。我直观地认为可能的一件事是选择二维的一行std::mdspan并将其分配给 a std::span,例如有点像
float* data = ...
std::mdspan matrix (data, 2, 5);
std::span vector = matrix[0]; // <-- should be a std::span<float, std::dynamic_extent> viewing row 0 of matrix
Run Code Online (Sandbox Code Playgroud)
经过一番研究,我没有找到一种明显直接的方法来实现这一目标,无论是使用 mdspan 的成员函数还是使用 std 库中的免费函数。目前我看到的唯一可能性是回到原始指针级别并编写自己的自由函数来执行此操作,这并不像我预期的那么优雅。我是否忽略了某些东西,或者这真的不是 mdspan 的功能吗?
我需要在着色器中使用两个纹理,一个在顶点着色器中,另一个在片段着色器中。在这两种情况下,它们都会在着色器中引用,例如uniform sampler2D tex1;或uniform sampler2D tex2;但是我不太确定如何正确使用相关的 GL 调用。
首先,如何创建这两个纹理?我需要像这样使用多个纹理单元吗
GLuint texIdx[2] = {0, 1};
GLuint texName[2];
GLint texUniformID[2];
// Initialize first texture
glActiveTexture (GL_TEXTURE0 + texIdx[0]);
glGenTextures (1, &texName[0]);
glBindTexture (GL_TEXTURE_2D, texName[0]);
glTexImage2D (GL_TEXTURE_2D, 0, GL_R32F, xDim0, yDim0, 0, GL_RED, GL_FLOAT, someTextureData);
// Initialize second texture
glActiveTexture (GL_TEXTURE0 + texIdx[1]);
glGenTextures (1, &texName[1]);
glBindTexture (GL_TEXTURE_2D, texName[1]);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, xDim1, yDim1, 0, GL_RGB, GL_FLOAT, someOther1TextureData);
// Set the uniforms to refer to the textures
texUniformID[0] …Run Code Online (Sandbox Code Playgroud) 我刚刚遇到一个案例,我想声明 C++ 20concept已弃用。然而,我的编译器(Apple Clang 14)似乎不接受[[deprecated]]向concept. 我的尝试看起来像
template <class T>
concept [[deprecated ("Some explanation")]] myConcept = someBoolCondition<T>;
Run Code Online (Sandbox Code Playgroud)
是否根本不支持弃用概念,我是否选择了错误的语法,或者这是我的编译器的缺陷?
我在 SO 上搜索了一下,很惊讶我没有找到任何类似的问题。如果已经回答了任何提示,我们很高兴。
我有一个定义了很多枚举类的代码库。其中一些指定了 totalNum 常量,例如
enum class Foo : int
{
a,
b,
c,
totalNum
}
Run Code Online (Sandbox Code Playgroud)
其他人没有这样的
enum class Bar : bool
{
oneOption,
otherOption
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个基本上像这样的功能
template <class EnumClassType>
EnumClassType typeToEnum (typename std::underlying_type<EnumClassType>::type value)
{
// If you hit this assertion, the value is outside of the valid enum range
assert (isPositiveAndBelow (value, decltype (value) (EnumClassType::totalNum)));
return EnumClassType (value);
}
Run Code Online (Sandbox Code Playgroud)
虽然这对totalNum指定的枚举有效并且有意义,但我想跳过这个断言,以防枚举中没有这样的标识符。有没有办法做到这一点?代码库目前使用 C++ 14,但由于即将进行的编译器更改,也欢迎使用 C++ 17 解决方案。
我有一个类,它包装不同类型的容器/视图,并且可以使用forEach成员函数调用所有元素上的函数。函数有不同的重载forEach,它们对函数类型(这是一个模板)的约束及其常量有所不同。现在我注意到,当调用具有参数的 lambda 的非常量实例时,auto&编译器错误地选择了 const 重载,从而无法编译。下面是一个说明问题的精简示例:
#include <type_traits>
#include <concepts>
#include <vector>
#include <span>
template <class, class>
struct FunctionWithSignatureOrImplicitlyConvertible : std::false_type {};
template <class Fn, class Ret, class... Args>
struct FunctionWithSignatureOrImplicitlyConvertible<Fn, Ret (Args...)>
{
static constexpr bool value = requires (Fn&& t, Args&&... args) { { t (args...) } -> std::convertible_to<Ret>; };
};
template <class Fn, class Signature>
concept functionWithSignatureOrImplicitlyConvertible = FunctionWithSignatureOrImplicitlyConvertible<Fn, Signature>::value;
template <class T>
struct Foo
{
using ValueType = typename T::value_type;
T& …Run Code Online (Sandbox Code Playgroud) 这是我在这里的第一个问题!
我即将将一些工作良好的 C++ 代码从 UNIX 移植到 Windows,它通过管道将 stdout 和 stderr 重定向到自定义 GUI 组件。我需要它来显示来自第三方库的反馈,该库仅将消息输出到我的 GUI 上的 stdout。
根据这个答案/sf/answers/43201091/这应该有效。事实上,链接中的代码在使用 Visual Studio 2017 构建的新命令行应用程序中按预期工作。但是在我现有的 GUI 应用程序中,对 的调用_fileno(stdout)以及对_fileno(stderr)两者的调用都返回 -2 而不是预期值 1 和 2,所以只需什么都没有发生。对相关函数进行硬编码 1 和 2 也不起作用。
所以谁能解释一下
供您参考,我收集了迄今为止在 Linux 和 Mac OS 上编程的主要经验,因此对于经验丰富的 Windows 人员来说,这可能是显而易见的。GUI 应用程序基于 JUCE 框架,因此我使用的是由 JUCE 工具 Projucer 创建的自动生成的 Visual Studio 项目
我想从我不维护的 Rust 项目生成一个静态库。该项目允许构建 Cargo.toml 指定的动态库 \xe2\x80\x94 crate-type = ["cdylib"]。
修改文件中的板条箱类型是可行的,但如果可能的话,我想将未修改的原始项目作为 git 子模块保留在我的项目中。
\n是否有任何标志可以传递给cargo build命令来覆盖此设置?
如果我在 CMake 中创建一个别名目标,例如
add_library(my::foo ALIAS my_foo)
Run Code Online (Sandbox Code Playgroud)
有没有办法从别名目标查询底层目标名称的名称?
我的用例:
CMakeList.txt要添加add_subdirectory到使用它的项目中的via。根据我们的惯例,egmy_foo将始终位于名为 的子文件夹中my_foo。此外,my_foo将作为别名目标导出my::foo并在项目中使用。请注意,这my_foo始终是一个INTERFACE目标,因此我无法在其上设置任何自定义属性。conanfile.txtmy::foo作为参数传递给该函数,但从my_foo函数内部的该参数派生以获取相应的文件夹名称来扫描 conanfile也欢迎任何根据我的用例解决问题的其他建议!
我想要实现什么
我尝试建立一个工具链来编译适用于英特尔 FPGA 的 OpenCL 应用程序。因此,在构建基于 C++ 的主机应用程序时,我需要为 OpenCL 内核调用英特尔 OpenCL 离线编译器。
仅当编辑了 cl 源文件或生成的二进制文件丢失时才应执行此步骤。我的方法是添加一个自定义命令来调用 CL 编译器并创建一个取决于该命令生成的输出的自定义目标。离线 Open CL 编译器被调用aoc,并且由于系统上可能存在多个 SDK 版本,我使用存储在aocExecutable. 这是我的 CMakeLists.txt 的相关部分
set (CLKernelName "vector_add")
set (CLKernelSourceFile "${PROJECT_SOURCE_DIR}/${CLKernelName}.cl")
set (CLKernelBinary "${PROJECT_BINARY_DIR}/${CLKernelName}.aocx")
add_executable (HostApplication main.cpp)
# ------ a lot of unneccessary details here ------
add_custom_command (OUTPUT "${CLKernelBinary}"
COMMAND "${aocExecutable} -march=emulator ${CLKernelSourceFile} -o ${CLKernelBinary}"
DEPENDS "${CLKernelSourceFile}"
)
add_custom_target (CompileCLSources DEPENDS "${CLKernelBinary}")
add_dependencies (HostApplication CompileCLSources)
Run Code Online (Sandbox Code Playgroud)
什么不起作用 在 Linux 下的 CLion IDE 中运行此命令会导致以下错误:
/bin/sh: 1: /home/me/SDKsAndFrameworks/intelFPGA/18.1/hld/bin/aoc -march=emulator …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的构建器类
class Builder
{
public:
Builder (SomeData someData);
template <class FooType, class... FooConstructorArgs>
Builder&& addFoo (FooConstructorArgs&&... args) &&
{
addInternal (new FooType (std::forward<FooConstructorArgs> (args)...);
return std::move (*this);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
现在有这样一个功能
std::string createUuid();
template <std::constructible_from<std::string> FooType>
void bar (SomeData data)
{
auto builder = Builder (data).addFoo<FooType> (createUuid());
// do something fancy with that builder
}
Run Code Online (Sandbox Code Playgroud)
然而,我真正想要的是这样的函数签名:
template <std::constructible_from<std::string>... FooTypes>
void bar (SomeData data)
Run Code Online (Sandbox Code Playgroud)
其中addFoo每种类型被称为链式,例如
addFoo<Bar, Baz> (data);
// expands internally to
auto builder = Builder (data).addFoo<Bar> (createUuid()).addFoo<Baz> (createUuid()); …Run Code Online (Sandbox Code Playgroud)