小编Jea*_*ier的帖子

在一行上获取 clang-format 格式的 require-expression

这是我的代码:

if constexpr (requires { { N::uuid() } -> std::convertible_to<score::uuid_t>; })
Run Code Online (Sandbox Code Playgroud)

clang-format (14, 15) 坚持将其格式化为 5 行:

if constexpr (requires {
                {
                  N::uuid()
                  } -> std::convertible_to<score::uuid_t>;
              })
Run Code Online (Sandbox Code Playgroud)

这太疯狂了,我怎样才能确保它保持在一行上而不必注释每一行?

c++ code-formatting clang-format c++20

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

概念和实际类型的模糊模板专业化:哪个编译器是正确的?

考虑以下代码:

#include<concepts>

template<typename>
void foo() { }

template<std::integral> 
void foo() { }

template<> 
void foo<bool>() { }

int main() { foo<bool>(); }
Run Code Online (Sandbox Code Playgroud)

它在最近的 Clang 和 MSVC 下编译没有问题,但在 GCC 下编译没有问题(godbolt 链接

使用 GCC 构建失败并显示:

error: ambiguous template specialization 'foo<bool>' for 'void foo()'
    9 | void foo<bool>() { }
      |      ^~~~~~~~~
note: candidates are: 'template<class> void foo()'
    3 | void foo() { }
      |      ^~~
note:                 'template<class>  requires  integral< <template-parameter-1-1> > void foo()'
    6 | void foo() { }
      | …
Run Code Online (Sandbox Code Playgroud)

c++ template-specialization language-lawyer explicit-specialization c++-concepts

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

模板上的begin()和end()自由函数重载

我有一个模板类,Iterable; 我想要重载begin()end()自由功能.它存储的数据作为vectorunique_ptr,但接口使用boost::indirect_iterator的便利性.

我的代码构建和运行CLang-3.5,但我试过g++-4.9,它没有.但我不知道为什么?(以及哪种编译器具有正确的行为).

template<typename T>
using SimpleVec = std::vector<T, std::allocator<T>>;

template <typename T,
          template <typename> class Container = SimpleVec,
          class String = std::string>
class Iterable
{
        template <typename friendT,
                  template <typename> class friendContainer,
                  class friendString>
        friend boost::indirect_iterator<typename friendContainer<std::unique_ptr<friendT>>::iterator>
            begin(Iterable<friendT, friendContainer, friendString>& i);

        template <typename friendT,
                  template <typename> class friendContainer,
                  class friendString>
        friend boost::indirect_iterator<typename friendContainer<std::unique_ptr<friendT>>::iterator>
            end(Iterable<friendT, friendContainer, friendString>& i);
};
Run Code Online (Sandbox Code Playgroud)

和免费功能:

template <typename T,
          template …
Run Code Online (Sandbox Code Playgroud)

templates boost non-member-functions c++11 c++14

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

如何逐行解析LLVM IR

我特别需要在我的c ++代码运行期间逐行解析LLVM IR代码,我需要知道每行上哪些操作数发生了什么操作.

例如,如果IR代码是:

%0 = load i32* %a, align 4
Run Code Online (Sandbox Code Playgroud)

我想知道在我的c ++代码运行期间%a加载的值%0.我曾考虑使用简单的文本解析c ++程序来执行此操作(解析IR并搜索IR关键字),但想知道是否有任何现有的库(可能来自LLVM本身)将帮助我避免这样做.

c++ parsing llvm llvm-ir

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

为什么-ansi和-std = c ++ 11在g ++中发生冲突?

为什么-ansi和-std = c ++ 11不能一起工作?(根据其他答案,ANSI恢复到C++ 98)我正在使用g ++ - 4.8.

这是ANSI批准的C++ 11:

http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2fISO%2fIEC+14882-2012

这让我感到困惑.谢谢!

g++ ansi c++11

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

macOS:模拟鼠标事件仅在启动二进制文件时有效,不适用于应用程序包

我有以下在 Mac 上移动鼠标光标的代码:

void moveCursorPos()
{
  CGPoint ppos;
  ppos.x = 100;
  ppos.y = 100;

  CGEventRef e = CGEventCreateMouseEvent(nullptr, kCGEventMouseMoved, ppos, kCGMouseButtonLeft);
  CGEventPost(kCGHIDEventTap, e);
  CFRelease(e);
}
Run Code Online (Sandbox Code Playgroud)

当我直接从其二进制文件运行我的软件时它可以工作 - 例如./foo.app/Contents/MacOS/foo。当我启动它时它不起作用open foo.app。做事open ./foo.app/Contents/MacOS/foo

当我第一次启动它并调用该函数时,macOS 询问我使用辅助功能 API(或类似的东西)的权限,我授予了该权限 - 如果我进入 macOS 安全设置的“辅助功能 > 隐私”窗格,所有复选框都被选中,一切都被授予foo,等等......

  • 如何在我的机器上修复它?
  • 我可以在代码方面做什么,以便我的软件的用户永远不会遇到该问题,因为它破坏了我的软件的核心 UI 交互?

macos accessibility core-graphics cursor-position

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

保持函数参数的保守性

我正在使用整洁的 fmt 库,该库在其版本 8 中会在编译时检查其格式字符串(如果编译器支持相关功能)。

在某些时候,我想编写以下代码:

throw my_exception("error: {}", 123);
Run Code Online (Sandbox Code Playgroud)

可悲的是,天真的实现:

struct my_exception : std::runtime_error {
  template<typename... Args>
  my_exception(Args&&... args)
    : std::runtime_error{fmt::format(std::forward<Args>(args)...)} 
  { }
};
Run Code Online (Sandbox Code Playgroud)

失败,因为这会失去字符串文字参数的“consteval-ness”,这是fmt::format. 目前,我决定以下几点:

template<std::size_t N>
struct literal {
  constexpr literal(const char (&str)[N]) noexcept {
    std::copy_n(str, N, this->str);
  }

  char str[N];
};

template<literal lit>
struct exception : std::runtime_error {
  template<typename... Args>
  exception(Args&&... args)
    : std::runtime_error{fmt::format(lit.str, std::forward<Args>(args)...)}
  {

  }
};
Run Code Online (Sandbox Code Playgroud)

被称为像

throw my_exception<"foo {}">(123);
Run Code Online (Sandbox Code Playgroud)

如何在保持编译时检查的同时恢复正常的函数调用语法?

c++ constexpr c++20 fmt consteval

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