推导第一个模板参数与其他默认模板参数 [英] Deducing first template argument with other template parameters defaulted
问题描述
Gcc和clang在此代码是否应该编译方面似乎有分歧:
Gcc and clang seem to disagree on whether this code should compile or not:
#include <type_traits>
template <typename Signature, int N = 0>
struct MyDelegate { };
template <typename D>
struct signature_traits;
template <template <typename> class Delegate, typename Signature>
struct signature_traits<Delegate<Signature>>
{
using type = Signature;
};
static_assert(std::is_same_v<
void(int, int),
signature_traits<MyDelegate<void(int, int)>>::type
>);
请参见 godbolt输出并尝试.我在这里使用clang,但是C ++标准对此有何看法?
See godbolt output here and try it. I'm siding with clang here, but what does the C++ standard say about this?
一个后续问题-可以使之适用于clang吗?
A follow-up question - can this be made to work in clang?
推荐答案
这是完全有效的代码,gcc是正确的. 功能"是C ++ 17中引入的 .这不是真正的功能,因为它是缺陷报告. MyDelegate
与signature_traits
的部分专业化匹配,因此应该像gcc一样正确地对待它.请注意,它之所以有效是因为默认使用第二个模板参数.
This is perfectly valid code, and gcc is right. The "feature" was introduced in C++17. It's not really a feature because it is a defect report. MyDelegate
matches the partial specialization of signature_traits
, and so it should be taken as gcc correctly does. Note that it works because the second template parameter is defaulted.
clang不编译的原因是因为该缺陷报告具有缺陷:P.它未在部分排序中引入适当的更改,不太好,使先前的有效代码又变得模棱两可.
The reason why clang doesn't compile it is because that defect report has a defect :P. It doesn't introduce the appropriate change in partial ordering, which is not really nice and makes previousy valid code ambiguous again.
预计它将很快修复,但是与此同时,clang决定隐藏"标志-frelaxed-template-template-args后面的功能.
It is expected to be fixed soon, but in the meanwhile, clang decided to "hide" the feature behind a flag, -frelaxed-template-template-args.
因此,只需在启用该标志的情况下进行编译,就可以了.
So, just compile with that flag enabled and you should be fine.
这篇关于推导第一个模板参数与其他默认模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!