这是我的代码:
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)
这太疯狂了,我怎样才能确保它保持在一行上而不必注释每一行?
考虑以下代码:
#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
我有一个模板类,Iterable
; 我想要重载begin()
和end()
自由功能.它存储的数据作为vector
的unique_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) 我特别需要在我的c ++代码运行期间逐行解析LLVM IR代码,我需要知道每行上哪些操作数发生了什么操作.
例如,如果IR代码是:
%0 = load i32* %a, align 4
Run Code Online (Sandbox Code Playgroud)
我想知道在我的c ++代码运行期间%a
加载的值%0
.我曾考虑使用简单的文本解析c ++程序来执行此操作(解析IR并搜索IR关键字),但想知道是否有任何现有的库(可能来自LLVM本身)将帮助我避免这样做.
为什么-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
这让我感到困惑.谢谢!
我有以下在 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
,等等......
我正在使用整洁的 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)
如何在保持编译时检查的同时恢复正常的函数调用语法?