gpt4 book ai didi

c++ - 无法理解的 с++ 编译器行为

转载 作者:行者123 更新时间:2023-12-02 18:33:53 24 4
gpt4 key购买 nike

也许我对 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com