C ++ 17模板推导指南不用于空参数集吗? [英] C++17 template deduction guide not used for empty parameter set?
问题描述
请考虑以下简化示例,也可以在 https://godbolt.org/g/Et56cm :
Consider the following reduced example which can also be viewed at https://godbolt.org/g/Et56cm:
#include <utility>
template <class T> struct success
{
T value;
constexpr success(T &&v)
: value(std::move(v))
{
}
constexpr success(const T &v)
: value(v)
{
}
};
template <> struct success<void>
{
};
template <class T> success(T /*unused*/)->success<T>;
success()->success<void>;
int main(void)
{
auto a = success{5}; // works
auto b = success{}; // works
auto c = success{"hello"}; // works
auto d = success(5); // works
auto e = success(); // FAILS!
auto f = success("hello"); // works
static_assert(std::is_same<decltype(a), success<int>>::value, "");
static_assert(std::is_same<decltype(b), success<void>>::value, "");
static_assert(std::is_same<decltype(c), success<const char *>>::value, "");
static_assert(std::is_same<decltype(d), success<int>>::value, "");
static_assert(std::is_same<decltype(e), success<void>>::value, "");
static_assert(std::is_same<decltype(f), success<const char *>>::value, "");
return 0;
}
令我惊讶的是 success()
无法编译,但成功{}
可以编译。我提供了模板推导指南 success()->成功< void>
,所以我会认为 success()
也可以。
What is surprising to me is that success()
does not compile, yet success{}
does. I have provided the template deduction guide success() -> success<void>
, so I would have thought that success()
would work as well.
这是C ++ 17标准中的预期行为,还是我缺少什么?
Is this expected behaviour in the C++ 17 standard, or am I missing something?
推荐答案
这是gcc错误(只需提交 81486 )。推导 success()
时,我们合成了一个过载集,其中包括:
This is a gcc bug (just filed 81486). When deducing success()
, we synthesize an overload set which consists of:
// from the constructors
template <class T> success<T> foo(T&& ); // looks like a forwarding reference
// but is really just an rvalue reference
template <class T> success<T> foo(T const& );
// from the deduction guides
template <class T> success<T> foo(T ); // this one is a bit redundant
success<void> foo();
并确定返回类型,就好像它被调用为 foo()
,这肯定会为您提供成功< void>
的类型。并不是错误。
And determine the return type as if it were invoked as foo()
, which certainly should give you a type of success<void>
. That it doesn't is a bug.
这篇关于C ++ 17模板推导指南不用于空参数集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!