如果我想制作一个可以接受 2 个untyped参数的模板,并通过do符号传递它们,当我省略第二个参数时,do我希望有一种方法以参数的默认值的形式指定回退。像这样:
template tpl(x: bool, body: untyped, bodyFinally: untyped): void =
if x: body
else: bodyFinally
#call site:
var r: int
tpl(true) do:
r = 2
do:
raise newException(Exception, "")
Run Code Online (Sandbox Code Playgroud)
这有效,但是:
template tpl(x: bool, body: untyped, bodyFinally: untyped = discard): void =
# same rest
Run Code Online (Sandbox Code Playgroud)
错误:应有表达式,但发现“关键字丢弃”
不接受默认值,消息很奇怪,discard是不是表达式。
尝试解决方法:
template tpl(x: bool, body: untyped, bodyFinally: untyped = proc()): void =
# same rest
Run Code Online (Sandbox Code Playgroud)
然后我们得到:
错误:表达式 'proc ()' 的类型为 'type proc (){.closure.}' 并且必须被丢弃 …
我对“原子约束”一章很好奇https://en.cppreference.com/w/cpp/language/constraints
它说
替换后 E 的类型必须恰好是 bool。不允许转换
和
f(0); // error: S<int>{} does not have type bool when checking #1,
// even though #2 is a better match
Run Code Online (Sandbox Code Playgroud)
哎哟。这意味着在使用 require 子句时没有 SFINAE 机制?这不是很糟糕吗?
因为我可以看到某些模板类型在遍历表达式后如何结果为 bool,但其他模板类型却看不到。现在我们又需要使用enable_if和东西了。痛得多?
我正在编写文档生成器并且正确地使用包含路径,所以当我解析文件时,我只是完全跳过每个包含.我还手动调整所有有问题的定义或#ifdef块,因为缺少包含(以及不同的命令行与生成版本)而被跳过.
我注意到的问题是:
struct ComplexBuffer : IAnimatable
{
};
Run Code Online (Sandbox Code Playgroud)
随着IAnimatable未声明(或向前声明).
我正在使用clang.cindex的python绑定,所以我使用get_children进行迭代:这个结果出来了:
Found grammar element "IAnimatable" {CursorKind.CLASS_DECL} [line=37, col=8]
Found grammar element "ComplexBuffer" {CursorKind.STRUCT_DECL} [line=39, col=9]
Run Code Online (Sandbox Code Playgroud)
如果我完成基本类型:
class IAnimatable {};
struct ComplexBuffer : IAnimatable
Run Code Online (Sandbox Code Playgroud)
我得到了正确的输出:
Found grammar element "IAnimatable" {CursorKind.CLASS_DECL} [line=37, col=8]
Found grammar element "ComplexBuffer" {CursorKind.STRUCT_DECL} [line=39, col=9]
Found grammar element "class IAnimatable" {CursorKind.CXX_BASE_SPECIFIER} [line=39, col=25]
Found grammar element "class IAnimatable" {CursorKind.TYPE_REF} [line=39, col=25]
Run Code Online (Sandbox Code Playgroud)
正是我想要的,因为我可以检测到继承列表放在文档中.
这个问题只是因为我跳过了所有的包含.
也许我可以通过手动重新解析声明行来解决这个问题?
编辑PS:为了完成我的解析python脚本:
import clang.cindex
index = clang.cindex.Index.create()
tu = …Run Code Online (Sandbox Code Playgroud) 在C ++ 17中,如何验证constexpr类型是否属于变量的类型列表?
例如:
using MyVt = std::variant<int, float>;
static_assert( MyVt::has_type< bool >::value, "oops, forgot bool");
Run Code Online (Sandbox Code Playgroud)
要么
static_assert( mpl::has_key< MyVt::typelist, T >::value, "oops, forgot T");
Run Code Online (Sandbox Code Playgroud)
当然,在概念表达式或static_assert模板函数中更有用。限制接受的可能类型。
如果我们不能为此使用显式支持的标准元功能或金属主义者,则可以使用涉及构造函数表达式的SFINAE破解检查吗?
有没有搞错 ?(此后报价后以粗体显示的真实问题)
§20.7.2.2.1
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
23要求:Y*应可转换为T*.24效果:构造一个与r共享所有权的shared_ptr对象,并存储r中存储的指针的副本.
25后置条件:use_count() == r.use_count().
26投掷:bad_weak_ptr何时r.expired().
27异常安全:如果抛出异常,则构造函数无效.
这不是助推行为.从过期的弱构造的共享给出一个空的共享.你可以在布尔上下文中测试它.
为什么委员会选择了例外的方式?例如,谷歌C++指南完全取消了异常使用.具有此类指导的项目,甚至在构建时禁用异常(在授权禁用的编译器上)将如何禁用?
最后,如果这可能经常发生(开发人员依赖过期的指针检测作为正常的程序流),那么它(对于实时程序)是不是很危险?我记得有一篇文章提到了实现异常的两种可能的策略,一种是减慢一切,但不是真的在异常发生时,另一种只有在异常发生时才会缓慢,但不会影响其余部分.我想这在某种程度上仍然必须成立.
echo 0.isNil
Run Code Online (Sandbox Code Playgroud)
类型不匹配:得到 <
intliteral (0)>但预期为以下之一:proc isNil[T: proc](x: T): bool
proc isNil[T](x: ptr T): bool
proc isNil(x: cstring) : bool
proc isNil(x: 指针): bool
proc isNil(x: string): bool
proc isNil[T](x: seq[T]): bool
proc isNil[T](x: ref T): bool
当我们不能有像这样的表达式时,我们应该如何编写通用代码0.isNil?