- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
也许我对 C++ 不太了解,但我发现 C++ 编译器的行为难以理解(在我看来是危险的)。
MSVC、g++ 和 Clang 的行为相同。
问:为什么要运行 ::a::f
可见为 f
里面b::f(bool)
?
namespace a {
struct C {
bool value;
C(): value(false) {}
C(C const &): value(false) {}
explicit C(bool value): value(value) {}
};
inline C f(bool value) { return C(value); }
// why this function is visible as `f` inside `b::f(bool)`?
inline C f(C const &value) { return C(not value.value); }
}
namespace b {
inline bool f(::a::C const &value) { return value.value; }
inline bool f(bool value) {
#ifdef WORK_AROUND_PROBLEM
return b::f(a::f(value));
#else
// why `::a::f` is visible as `f` here?
return f(a::f(value)).value;
#endif
}
}
int main(int, char **) {
if (b::f(false) or (! b::f(true))) return 1;
if (b::f(0)) return 2;
if (b::f(a::C(false)) || (! b::f(a::C(true)))) return 3;
return 0;
}
========
更新========
第一个代码示例可以用 Argument dependent lookup
来解释https://en.cppreference.com/w/cpp/language/adl 。感谢您的回答。 =)
但我仍然不清楚如何正确处理一组“通用函数”。在第一个示例的上下文中,我无法弄清楚如何正确替换 f (C const &)
与 template <class ... T> auto f (T && ...)
作为下一个示例 [c++20],我在这里发布了一段更真实的代码(但经过简化)。这是仅头文件库的一部分。在这里我想要一个通用make
::exception::Walker
的功能。和通用make
exception::Backtrace
的功能。此外,我希望 ::exception::walker
之间有最小的连接。和::exception::backtrace
命名空间。请参阅下面代码中的注释。
#include <cstdint>
#include <list>
#include <ranges>
#include <utility>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <exception>
#include <stdexcept>
#include <type_traits>
// from <.../exception/walker.fwd.hpp>
namespace exception {
namespace walker {
struct Class;
struct Iterator;
using Value = ::std::exception_ptr;
template <class ... T> auto make(T && ...);
} // namespace walker
using Walker = walker::Class;
} // namespace exception
// from <.../exception/backtrace.fwd.hpp>
namespace exception {
namespace backtrace {
using Item = ::std::exception_ptr;
using Class = ::std::vector<Item>;
auto make();
template <class T> auto make(T &&source);
template <class beginT, class endT> auto make(beginT &&begin, endT &&end);
} // namespace backtrace
using BackTrace = backtrace::Class;
} // namespace exception
// from <.../exception/walker.hpp>
namespace exception::walker {
struct Class final {
using Value = walker::Value;
using Iterator = walker::Iterator;
auto begin() const noexcept(true);
auto end() const noexcept(true);
Class() noexcept(true) = default;
Class(Class &&) noexcept(true) = default;
Class(Class const &) noexcept(true) = default;
template <class headT, class ... tailT> requires((0 < sizeof ... (tailT)) or (not ::std::is_base_of_v<Class, ::std::decay_t<headT>>))
explicit Class(headT &&head, tailT && ... tail) noexcept(true);
private:
Value last_;
// There was a lot of overloads
template <class T> decltype(auto) make_value_(T &&value) noexcept(true);
};
struct Iterator final {
using iterator_category = ::std::input_iterator_tag;
using difference_type = ::std::ptrdiff_t;
using value_type = Value;
using pointer = Value *;
using reference = Value &;
inline auto & operator*() const noexcept(true) { return value_; }
inline auto * operator->() const noexcept(true) { return &value_; }
inline auto & operator++() noexcept(true) {
auto const temporary_ = value_;
if (static_cast<bool>(temporary_)) {
value_ = {};
try { ::std::rethrow_exception(temporary_); }
catch(::std::exception const &exception_) {
try { ::std::rethrow_if_nested(exception_); }
catch (...) { value_ = ::std::current_exception(); }
}
catch (...) {}
}
return *this;
}
inline auto operator++(int) noexcept(true) { auto const temporary_ = *this; ++(*this); return temporary_; }
inline auto operator==(Iterator const &other) const noexcept(true) { return value_ == other.value_; };
inline auto operator!=(Iterator const &other) const noexcept(true) { return not (*this == other); };
Iterator & operator=(Iterator &&) noexcept(true) = default;
Iterator & operator=(Iterator const &) noexcept(true) = default;
Iterator() noexcept(true) = default;
Iterator(Iterator &&) noexcept(true) = default;
Iterator(Iterator const &) noexcept(true) = default;
inline explicit Iterator(Value const &value) noexcept(true): value_{value} {}
private:
value_type value_;
};
inline auto Class::begin() const noexcept(true) { return Iterator{last_}; }
inline auto Class::end() const noexcept(true) { return Iterator{}; }
template <class T> inline decltype(auto) Class::make_value_(T &&value) noexcept(true) {
if constexpr (::std::is_same_v<::std::exception_ptr, ::std::decay_t<decltype(value)>>) return ::std::forward<decltype(value)>(value);
else return ::std::make_exception_ptr(::std::forward<decltype(value)>(value));
}
template <class headT, class ... tailT> requires((0 < sizeof ... (tailT)) or (not ::std::is_base_of_v<Class, ::std::decay_t<headT>>))
inline Class::Class(headT &&head, tailT && ... tail) noexcept(true): last_{::exception::walker::Class::make_value_(::std::forward<headT>(head), ::std::forward<tailT>(tail) ...)} {}
template <class ... T> inline auto make(T && ... payload) { return Class{::std::forward<T>(payload) ...}; }
} // namespace exception::walker
// from <.../exception/backtrace.hpp>
namespace exception::backtrace {
namespace private_ {
// in fact, it would be more correct to use const lvalue to T (T const &source) instead universal reference (T &&), but then
// ::other::namespace::make function may be used (::exception::walker::make for example if ::std::decay_t<sourceT> will be ::exception::walker::Class)
template <class sourceT> inline auto make(void const *, sourceT const &source) {
using CollectorItem = ::std::decay_t<decltype(*::std::begin(source))>;
auto collector_ = ::std::list<CollectorItem>{};
auto size_ = static_cast<::std::size_t>(0);
for (auto const &exception_ : source) { size_++; collector_.push_front(exception_); }
auto result_ = Class(size_);
::std::copy(collector_.begin(), collector_.end(), result_.begin());
return result_;
}
template <class T> inline auto make(::std::exception const *, T &&exception) {
// sad, but without knowledge about content of namespace in which the real type of T is located, I have to write here explicitly ::exception::backtrace::private_::make
return ::exception::backtrace::private_::make(static_cast<void const *>(nullptr), ::exception::walker::make(::std::forward<T>(exception)));
}
template <class T> inline auto make(::std::exception_ptr const *, T &&exception) {
// sad, without knowledge about content of namespace in which the real type of T is located, I have to write here explicitly ::exception::backtrace::private_::make
return ::exception::backtrace::private_::make(static_cast<void const *>(nullptr), ::exception::walker::make(::std::forward<T>(exception)));
}
} // namespace private_
template <class T> inline auto make(T &&source) {
// sad, but without knowledge about content of namespace in which the real type of T is located, I have to write here explicitly ::exception::backtrace::private_::make
return ::exception::backtrace::private_::make(static_cast<::std::decay_t<T> const *>(nullptr), ::std::forward<T>(source));
}
template <class beginT, class endT> inline auto make(beginT &&begin, endT &&end) {
// sad, but without knowledge about content of namespace in which the real type of ::std::ranges::subrange result is located, I have to write here explicitly ::exception::backtrace::make
return ::exception::backtrace::make(::std::ranges::subrange(::std::forward<beginT>(begin), ::std::forward<endT>(end)));
}
} // namespace exception::backtrace
namespace test_ {
inline static auto internal() { throw ::std::runtime_error{"internal function error"}; }
inline static auto external() {
try { internal(); }
catch(...) { ::std::throw_with_nested(::std::runtime_error{"external function error"}); }
}
}
int main(int, char **) {
try { test_::external(); }
catch(...) {
for (auto const &exception_: ::exception::backtrace::make(::std::current_exception())) {
try { ::std::rethrow_exception(exception_); }
catch(::std::exception const &exception_) { ::std::clog << exception_.what() << ::std::endl << ::std::flush; }
catch(...) { ::std::clog << "unknown exception" << ::std::endl << ::std::flush; }
}
}
return 0;
}
最佳答案
这是由于Argument dependent lookup .
如果函数采用自定义类型的参数,则在调用函数时会在声明该类型的命名空间中搜索可行的重载。
f(a::f(value))
正在将 C
传递给 f
。这会触发 ADL,因此除了在周围范围中进行名称查找之外,编译器还会在 namespace a
中进行搜索,因为 C
是在那里声明的。
这是定义自由函数运算符重载时最常见的一个重要功能。我们可以将它们放在与相关类型相同的命名空间中,ADL
将确保在名称查找时找到重载,而不会污染全局/外部作用域。
关于c++ - 无法理解的 с++ 编译器行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69070867/
我通过 spring ioc 编写了一些 Rest 应用程序。但我无法解决这个问题。这是我的异常(exception): org.springframework.beans.factory.BeanC
我对 TestNG、Spring 框架等完全陌生,我正在尝试使用注释 @Value通过 @Configuration 访问配置文件注释。 我在这里想要实现的目标是让控制台从配置文件中写出“hi”,通过
为此工作了几个小时。我完全被难住了。 这是 CS113 的实验室。 如果用户在程序(二进制计算器)结束时选择继续,我们需要使用 goto 语句来到达程序的顶部。 但是,我们还需要释放所有分配的内存。
我正在尝试使用 ffmpeg 库构建一个小的 C 程序。但是我什至无法使用 avformat_open_input() 打开音频文件设置检查错误代码的函数后,我得到以下输出: Error code:
使用 Spring Initializer 创建一个简单的 Spring boot。我只在可用选项下选择 DevTools。 创建项目后,无需对其进行任何更改,即可正常运行程序。 现在,当我尝试在项目
所以我只是在 Mac OS X 中通过 brew 安装了 qt。但是它无法链接它。当我尝试运行 brew link qt 或 brew link --overwrite qt 我得到以下信息: ton
我在提交和 pull 时遇到了问题:在提交的 IDE 中,我看到: warning not all local changes may be shown due to an error: unable
我跑 man gcc | grep "-L" 我明白了 Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more inf
我有一段代码,旨在接收任何 URL 并将其从网络上撕下来。到目前为止,它运行良好,直到有人给了它这个 URL: http://www.aspensurgical.com/static/images/a
在过去的 5 个小时里,我一直在尝试在我的服务器上设置 WireGuard,但在完成所有设置后,我无法 ping IP 或解析域。 下面是服务器配置 [Interface] Address = 10.
我正在尝试在 GitLab 中 fork 我的一个私有(private)项目,但是当我按下 fork 按钮时,我会收到以下信息: No available namespaces to fork the
我这里遇到了一些问题。我是 node.js 和 Rest API 的新手,但我正在尝试自学。我制作了 REST API,使用 MongoDB 与我的数据库进行通信,我使用 Postman 来测试我的路
下面的代码在控制台中给出以下消息: Uncaught DOMException: Failed to execute 'appendChild' on 'Node': The new child el
我正在尝试调用一个新端点来显示数据,我意识到在上一组有效的数据中,它在数据周围用一对额外的“[]”括号进行控制台,我认为这就是问题是,而新端点不会以我使用数据的方式产生它! 这是 NgFor 失败的原
我正在尝试将我的 Symfony2 应用程序部署到我的 Azure Web 应用程序,但遇到了一些麻烦。 推送到远程时,我在终端中收到以下消息 remote: Updating branch 'mas
Minikube已启动并正在运行,没有任何错误,但是我无法 curl IP。我在这里遵循:https://docs.traefik.io/user-guide/kubernetes/,似乎没有提到关闭
每当我尝试docker组成任何项目时,都会出现以下错误。 我尝试过有和没有sudo 我在这台机器上只有这个问题。我可以在Mac和Amazon WorkSpace上运行相同的容器。 (myslabs)
我正在尝试 pip install stanza 并收到此消息: ERROR: No matching distribution found for torch>=1.3.0 (from stanza
DNS 解析看起来不错,但我无法 ping 我的服务。可能是什么原因? 来自集群中的另一个 Pod: $ ping backend PING backend.default.svc.cluster.l
我正在使用Hibernate 4 + Spring MVC 4当我开始 Apache Tomcat Server 8我收到此错误: Error creating bean with name 'wel
我是一名优秀的程序员,十分优秀!