我有一个问题,这是非常相似this之一。不一致有条件noexcept和过载
总之,我有一个magic
方法,这是noexcept
如果另一种方法是noexcept
。
奇怪的是,这种“的另一种方法”,有两个过载,并且编译器选择了第二超负荷确定magic
noexcept
-ness。
然而,当magic
被称为后来,第一超载被调用,但noexcept
-ness的magic
保持不变!
这里是wandbox link
从我的理解:
noexcept(magic(dummy2{}))
电话noexcept(noexcept(adl_caller(...))
其回落到adl_caller(..., priority_tag<0>) noexcept
因为user_method(dummy2)
不受此时编译器知道。
不够公平,但是,user_method(dummy2)
如何调用3行以上? 这是标准打算的吗?
很抱歉,如果我不够清晰。
#include <iostream>
template <unsigned N> struct priority_tag : priority_tag<N - 1> {};
template <> struct priority_tag<0> {};
template <typename T>
auto adl_caller(T t, priority_tag<1>) noexcept(noexcept(user_method(t)))
-> decltype(user_method(t)) {
std::cout << "first adl_caller overload" << std::endl;
user_method(t);
}
// tricky noexcept ...
template <typename T> void adl_caller(T, priority_tag<0>) noexcept {
std::cout << "second adl_caller overload" << std::endl;
}
template <typename T>
void magic(T t) noexcept(noexcept(adl_caller(t, priority_tag<1>{}))) {
adl_caller(t, priority_tag<1>{});
}
struct dummy {};
struct dummy2 {};
// un-commenting this line makes the above call to cout print '0'
// void user_method(dummy2);
void user_method(dummy)
{
// user_method(dummy2) is declared after this point
// this line prints '1', since magic falls back to the second adl_caller overload
std::cout << "noexcept?: " << noexcept(magic(dummy2{})) << std::endl;
std::cout << "dummy method called" << std::endl;
// however, the first adl_caller overload is called here ...
magic(dummy2{});
}
void user_method(dummy2)
{
std::cout << "dummy2 method called" << std::endl;
}
int main()
{
magic(dummy{});
}
我想我理解这些段落的某些部分,但它们对我来说有点太模糊。在我的示例中,我只有一个TU并且没有外部链接功能,与代码I链接的最后一段如何?如果我理解正确,'魔术'方法与第一段相关,但我有一些问题需要理解确切的原因。尽管如此,非常感谢标准摘录 – Dante