【发布时间】:2018-10-27 10:45:55
【问题描述】:
问题
我写了一段复杂的模板代码,可以编译with GCC 8.2.1,但不能编译with Clang 7.0(代码和错误链接)。
我认为这可能是this Q&A 的暗示,但我看不到。
动机
我正在编写一个类,我希望可以使用两个不同类型的可调用对象来构造它,但也可以省略其中一个,即:
my_class(callable_1);
my_class(callable_2);
my_class(callable_1, callable_2);
这应该没有问题。但是,为什么不允许callable_1 和callable_2 成为函数模板(或带有operator() 模板的函子)。也就是说,我想要这个(或至少最初想要):
my_class([](auto arg) {});
my_class([](auto arg) {});
my_class([](auto arg) {}, [](auto arg) {});
如您所见,不幸的是,两个可调用对象具有相同的签名,因此我们需要以某种方式消除它们之间的歧义。我能想到的第一种方法(也是这个问题所涉及的方法)是将“标签”参数添加到一个一元重载中:
my_class([](auto arg) {});
my_class([](auto arg) {}, callable_2_tag());
my_class([](auto arg) {}, [](auto arg) {});
在我看来,这看起来可以接受,但我想出了更好的解决方案:
- 在第二个可调用对象的签名(最后一个参数或返回类型)中使用标签(如果没有歧义,则可选)
- 将第二个构造函数重载为不同名称的非成员或
static成员函数
不过,我还是想知道,为什么在我最初的方法中两个编译器的行为存在差异,哪个是正确的(或者两者是否都是)。
代码:
为简单起见,我已将构造函数重载转换为常规 my_class 函数重载。
#include <iostream>
#include <type_traits>
// parameter types for callbacks and the tag class
struct foo { void func1() {} };
struct bar { void func2() {} };
struct bar_tag {};
// callable checks
template <typename Func>
static constexpr bool is_valid_func_1_v = std::is_invocable_r_v<void, Func, foo>;
template <typename Func>
static constexpr bool is_valid_func_2_v = std::is_invocable_r_v<void, Func, bar>;
// default values
static constexpr auto default_func_1 = [](foo) {};
static constexpr auto default_func_2 = [](bar) {};
// accepting callable 1
template <typename Func1, std::enable_if_t<is_valid_func_1_v<Func1>>* = nullptr>
void my_class(Func1&& func_1)
{
my_class(std::forward<Func1>(func_1), default_func_2);
}
// accepting callable 1
template <typename Func2, std::enable_if_t<is_valid_func_2_v<Func2>>* = nullptr>
void my_class(Func2&& func_2, bar_tag)
{
my_class(default_func_1, std::forward<Func2>(func_2));
}
// accepting both
template <
typename Func1, typename Func2,
// disallow Func2 to be deduced as bar_tag
// (not even sure why this check did not work in conjunction with others,
// even with GCC)
std::enable_if_t<!std::is_same_v<Func2, bar_tag>>* = nullptr,
std::enable_if_t<is_valid_func_1_v<Func1> &&
is_valid_func_2_v<Func2>>* = nullptr>
void my_class(Func1&& func_1, Func2&& func_2)
{
std::forward<Func1>(func_1)(foo());
std::forward<Func2>(func_2)(bar());
}
int main()
{
my_class([](auto foo) { foo.func1(); });
my_class([](auto bar) { bar.func2(); }, bar_tag());
}
对于 Clang,这将导致:
error: no member named 'func1' in 'bar'
my_class([](auto foo) { foo.func1(); });
~~~ ^
...
note: in instantiation of variable template specialization
'is_valid_func_2_v<(lambda at prog.cc:41:14)>' requested here
template <typename Func2, std::enable_if_t<is_valid_func_2_v<Func2>>* = nullptr>
^
这里发生了什么? 替换失败是个错误?
编辑:我完全不知道认为std::enable_if 谓词中的错误也会被静音......那是不是替代失败。
修复:
如果我将 SFINAE 作为函数参数,Clang 可以很好地处理它。 我不知道为什么将检查从模板参数推导阶段推迟到重载解决阶段会有所不同。
template <typename Func2>
void my_class(Func2&& func_2, bar_tag,
std::enable_if_t<is_valid_func_2_v<Func2>>* = nullptr)
{
my_class(default_func_1, std::forward<Func2>(func_2));
}
总而言之,我对通用性的研究可能超出了我的知识范围,现在我正在为此付出代价。那么我错过了什么?细心的读者可能会注意到弹出了一些附带问题,但我不想为所有这些问题提供答案。最后,如果可以制作更简单的 MCVE,我很抱歉。
【问题讨论】:
-
如果你用
my_class([](foo f) { f.func1(); })调用它是否有效?我无法从 cppreference 描述中看出is_invocable_r在您现有情况下的预期行为。事实上,无论哪种方式,我都预料到会出现编译错误。 -
使用 g++ head 9.0.0 2018(在 Wandbox 中尝试)我得到与 clang++ 相同的错误;不是专家,但我认为 clang++ 和最近的 g++ 是正确的。
-
我认为您使用
is_invokable_r的方式不太正确 SFINAE。看到这个:godbolt.org/z/cYtb8p。我认为任何编译器都会接受它的唯一原因是它实际上没有在您的foolambda 上实例化is_valid_func_2。 -
有趣的问题。 This 是一个更简单的程序,可以(可能)重现您的问题。
-
反正我问了new question。
标签: c++ templates language-lawyer c++17 sfinae