gpt4 book ai didi

c++ - std 类型别名的自定义点

转载 作者:行者123 更新时间:2023-12-04 08:25:23 31 4
gpt4 key购买 nike

假设我正在 lib 命名空间中编写一些通用算法,调用自定义点 my_func

第一次尝试是为 my_func 使用 ADL其中一位用户想要为他的类型专门化 my_func,这是 std 类型的别名。当然在他的命名空间中定义它是行不通的,因为 ADL 对别名不起作用。标准不允许在 std 命名空间中定义它。剩下的唯一选项似乎在算法的命名空间 lib 中定义。但如果最终用户在包含自定义 header 之前包含算法 header ,这也不起作用。

#include <iostream>
#include <array>

// my_algorithm.hpp
namespace lib{

template<typename T>
void my_algorithm(const T& t){
my_func(t);
}

} // namespace lib

// user1.hpp
namespace user1{

struct Foo1{
// this is working as expected (ADL)
friend void my_func(const Foo1&){
std::cout << "called user1's customisation\n";
}
};

} // namespace user1

// user2.hpp
namespace user2{

using Foo2 = std::array<int,1>;

// this won't work because Foo2 is actually in std namespace
void my_func(const Foo2&){
std::cout << "called user2's customisation\n";
}

} // namespace user2

/* surely this isn't allowed
namespace std{
void my_func(const user2::Foo2&){
std::cout << "called user2's customisation\n";
}
} //namespace std
*/

// another attempt to costomize in the algorithm's namespace
// this won't work because my_func isn't seen before my_algorithm
namespace lib{
void my_func(const user2::Foo2&){
std::cout << "called user2's customisation\n";
}
}



// main.cpp
// #include "algorithm.hpp"
// #include "user1.hpp"
// #include "user2.hpp"
int main(){
lib::my_algorithm(user1::Foo1{});
lib::my_algorithm(user2::Foo2{});
}

https://godbolt.org/z/bfdP8s

第二次尝试使用 niebloids 作为 my_func,这与 ADL 有同样的问题。

第三次尝试使用tag_invoke,它应该和ADL有同样的问题,即

  • 在用户命名空间中自定义将不起作用,因为我的类型是 std 类型的别名
  • 不允许在 std 中自定义
  • lib 命名空间中的自定义取决于 header 包含的顺序第一点似乎是正确的,但最后一点不是。这似乎有效
#include <iostream>
#include <array>

// tag_invoke.hpp overly simplified version
namespace lib_ti{

inline namespace tag_invoke_impl{

inline constexpr struct tag_invoke_fn{

template<typename CP, typename... Args>
decltype(auto) operator()(CP cp, Args&&... args) const{
return tag_invoke(cp, static_cast<Args&&>(args)...);
}

} tag_invoke{};

} // namespace tag_invoke_impl
} // namespace lib_to


// my_algorithm.hpp

// #include "tag_invoke.hpp"
namespace lib{

inline constexpr struct my_func_fn {

template <typename T>
void operator()(const T& t) const{
lib_ti::tag_invoke(*this, t);
}

} my_func{};


template<typename T>
void my_algorithm(const T& t){
my_func(t);
}

} // namespace lib

// user1.hpp
namespace user1{

struct Foo1{
// this is working as expected (ADL)
friend void tag_invoke(lib::my_func_fn, const Foo1&){
std::cout << "called user1's customisation\n";
}
};

} // namespace user1

// user2.hpp
namespace user2{

using Foo2 = std::array<int,1>;

// this won't work because Foo2 is actually in std namespace
void tag_invoke(lib::my_func_fn, const Foo2&){
std::cout << "called user2's customisation\n";
}

} // namespace user2

/* surely this isn't allowed
namespace std{
void tag_invoke(lib::my_func_fn, const user2::Foo2&){
std::cout << "called user2's customisation\n";
}
} //namespace std
*/

// another attempt to customise in the algorithm's namespace
// In ADL case, this does not work. But in this case, it seems to work. why?
namespace lib{
void tag_invoke(lib::my_func_fn, const user2::Foo2&){
std::cout << "called user2's customisation\n";
}
}



// main.cpp
int main(){
lib::my_algorithm(user1::Foo1{});
lib::my_algorithm(user2::Foo2{});
}

https://godbolt.org/z/hsKbKE

为什么这没有与第一个(原始 ADL)相同的问题?

第四次尝试使用模板特化,这似乎按预期正常工作

#include <iostream>
#include <array>




// my_algorithm.hpp

namespace lib{

template<typename T, typename = void>
struct my_func_impl{
//void static apply(const T&) = delete;
};

inline constexpr struct my_func_fn {

template <typename T>
void operator()(const T& t) const{
using impl = my_func_impl<std::decay_t<T>>;
impl::apply(t);
}

} my_func{};


template<typename T>
void my_algorithm(const T& t){
my_func(t);
}

} // namespace lib

// user1.hpp
namespace user1{

struct Foo1{};

} // namespace user1

namespace lib{

template<>
struct my_func_impl<user1::Foo1>{
void static apply(const user1::Foo1&){
std::cout << "called user1's customisation\n";
}
};

} //namespace lib



// user2.hpp
namespace user2{

using Foo2 = std::array<int,1>;

} // namespace user2

namespace lib{

template<>
struct my_func_impl<user2::Foo2>{
void static apply(const user2::Foo2&){
std::cout << "called user2's customisation\n";
}
};

}



// main.cpp
int main(){
lib::my_algorithm(user1::Foo1{});
lib::my_algorithm(user2::Foo2{});
}

https://godbolt.org/z/r71x6c


编写通用算法和自定义点并允许客户自定义 std 类型的别名的最佳方法是什么?

最佳答案

one of the user wants to specialise my_func for his type, which is an alias to std type

这就是原罪,它给你带来了所有的痛苦。 C++ 中的类型别名只是别名;它们不是新类型。您有一个使用自定义点的通用算法,例如

// stringify_pair is my generic algorithm; operator<< is my customization point
template<class T>
std::string stringify_pair(K key, V value) {
std::ostringstream oss;
oss << key << ':' << value;
return std::move(oss).str();
}

你的用户想用标准类型调用这个泛型算法,比如

std::string mykey = "abc";
std::optional<int> myvalue = 42;
std::cout << stringify_pair(mykey, myvalue);

这不起作用,因为 std::optional<int>不提供 operator<< .它不可能工作,因为您的用户拥有 std::optional<int>类型,因此无法向其添加操作。 (从物理上讲,他们当然可以尝试;但从哲学的角度来看,这是行不通的,这就是为什么每次(物理上)接近时你都会遇到障碍。)

用户使他们的代码工作的最简单方法是让他们“获得类型定义的合法所有权”,而不是依赖其他人的类型。

struct OptionalInt {
std::optional<int> data_;
OptionalInt(int x) : data_(x) {}
friend std::ostream& operator<<(std::ostream&, const OptionalInt&);
};
OptionalInt myvalue = 42; // no problem now

你问为什么tag_invoke没有与原始 ADL 相同的问题。我相信答案是当您调用 lib::my_func(t) 时,它调用 lib_ti::tag_invoke(*this, t) , 它对 tag_invoke(lib::my_func, t) 进行 ADL 调用,它正在使用包含您的 t 的参数列表执行 ADL (这并不重要)类型为 lib::my_func_fn 的第一个参数(这意味着 lib 是此调用的关联 namespace )。这就是它找到 tag_invoke 的原因重载你投入 namespace lib .

在原始 ADL 案例中,namespace lib 不是调用my_func(t)的关联命名空间. my_func重载你投入 namespace lib没有找到,因为它没有被 ADL 找到(不在关联的命名空间中),也没有被常规的不合格查找找到(因为模糊地挥手两阶段查找)。


What is the best way to write generic algorithms and customisation points and allow clients to customise for aliases for std types?

不要。类型的“接口(interface)”——它支持什么操作,你可以用它做什么——在类型作者的控制之下。如果您不是该类型的作者,请不要向其添加操作;相反,创建您自己的类型(可能通过继承,最好是通过组合)并为其提供您想要的任何操作。

在最坏的情况下,您最终会在程序的不同部分遇到两个不同的用户,一个在做

using IntSet = std::set<int>;
template<> struct std::hash<IntSet> {
size_t operator()(const IntSet& s) const { return s.size(); }
};

还有一个在做

using IntSet = std::set<int>;
template<> struct std::hash<IntSet> {
size_t operator()(const IntSet& s, size_t h = 0) const {
for (int i : s) h += std::hash<int>()(i);
return h;
}
};

然后他们都尝试使用 std::unordered_set<IntSet> ,然后当您传递 std::unordered_set<IntSet>boom、ODR 违规和运行时的未定义行为从一个目标文件到另一个目标文件,他们就 std::hash<std::set<int>> 的名称达成一致但不同意其含义。这只是一大堆蠕虫。不要打开它。

关于c++ - std 类型别名的自定义点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65286657/

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