今天我在看clang的C++17支持页面。我注意到了一些奇怪的事情。 将模板模板参数匹配到兼容参数 (P0522R0) 功能被标记为部分,因为它必须通过开关激活。他们的笔记says :
Despite being the the resolution to a Defect Report, this feature is disabled by default in all language versions, and can be enabled explicitly with the flag -frelaxed-template-template-args in Clang 4. The change to the standard lacks a corresponding change for template partial ordering, resulting in ambiguity errors for reasonable and previously-valid code. This issue is expected to be rectified soon.
当这个功能被激活时,什么样的构造会中断?为什么它会破坏代码以及如何破坏代码?
最佳答案
你可以有这样的代码:
template<template<typename> typename>
struct Foo {};
template<typename, typename = void>
struct Bar {};
Foo<Bar> unused;
如果没有缺陷解决方案,unused
将是格式错误的,因为 foo
采用的模板只有一个模板参数,而不是两个。如果你依赖这个(也许是 SFINAE):
template<template<typename> typename>
void foo();
template<template<typename, typename> typename>
void foo();
template<typename, typename = void>
struct Bar {};
int main() {
foo<Bar>(); // ambiguous after resolution!
}
那么调用就会失败!问题是偏序没有相应的变化,所以两个候选函数的生存能力都一样,而且调用不明确。
关于c++ - P0522R0如何破码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47293162/