-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can I make safe template dynamic methods? #14
Comments
As i understand impl AsRef here is a some type which has member function .as_ref And second way - type erase second argument too. |
No, no. I need I want this: template<typename Self>
struct Foo {
static void do_invoke(const Self& self, std::span<const std::byte> bytes) {
self.foo(bytes);
}
};
using any_foo = aa::any_with<Foo>;
auto bar(any_foo self, const auto& bytes) {
return aa::invoke<Foo>(self, std::as_bytes(std::span(bytes)));
}
struct A {
void foo(std::span<const std::byte> bytes) const {
for (auto&& b : bytes) {
std::cout << static_cast<std::size_t>(b) << " ";
}
std::cout << std::endl;
}
};
auto main() -> int {
auto a = A{};
bar(a, std::string("hello"));
bar(a, "hello");
bar(a, u8"hello");
bar(a, std::to_array<std::uint8_t>({104, 101, 108, 108, 111}));
} |
Okay, then you can use plugins: template<typename Self>
struct Foo {
static void do_invoke(const Self& self, std::span<const std::byte> bytes) {
self.foo(bytes);
}
template<typename CRTP>
struct plugin {
auto bar(const auto& bytes) const {
return aa::invoke<Foo>(*static_cast<const CRTP*>(this), std::as_bytes(std::span(bytes)));
}
};
};
struct A {
void foo(std::span<const std::byte> bytes) const {
for (auto&& b : bytes) {
std::cout << static_cast<std::size_t>(b) << " ";
}
std::cout << std::endl;
}
};
auto main() -> int {
auto value = A{};
// more effective then create polymorphic value like any_foo
aa::const_poly_ref<Foo> a = value;
a.bar("hello");
a.bar(u8"hello");
a.bar(std::to_array<std::uint8_t>({104, 101, 108, 108, 111}));
} https://godbolt.org/z/1TeM7TPTj |
Ok, thanks, that works) |
Can you please tell me is that code in rust does same thing as C++? |
I don't know. This is a new hype feature.
This is because of borrow semantic, it is not so easy to explain for byte /* : &u8 */ in bytes.iter() {
result = result + *byte as usize;
}
// ----
for byte /* : u8 */ in bytes.iter().cloned() {
result = result + byte as usize;
} P.S. code in rust does same thing as C++ (almost) |
In rust exit code can be only |
but i can return i32 in std::process:exit. |
I make very rough benchmarks (100 million calls per sec): I can rewrite bench: // from
aa::any_with<Foo> a = value;
// to
aa::any_with<Foo> b = value;
auto /* poly_ref */ a = *&b; Why |
Can i see all code? May be you allocate every time because your type has throw move constructor or smth |
Sure, without IO. I use tiny example. Also, if you use the functions (insted of macro bench), then |
I think it's just an accident of clang limits for inlining and the benchmark is designed so that the lack of an inline call guarantees insanely slow results. |
This is strange, template function does not erase type info |
I just rebench and think |
i will try, maybe compiler thinks invoke can change state of any_with and dont assumes that pointer will be same. Need to see assembly... |
why you use virtual functions in second case? Its double erase or smth. |
This is to test compiler optimization in calls of raw virtual functions. If you talk about |
No, but godbolt uses same xeons (I hope)
Uh, this is known bug. Please add |
Okay, my last words about it... May be https://godbolt.org/z/oc1YKahc8 Looks like the results are different without it |
Oh, my Jesus. What is it About fn black_box<T>(dummy: T) -> T {
unsafe {
let ret = std::ptr::read_volatile(&dummy);
std::mem::forget(dummy);
ret
}
} https://stackoverflow.com/questions/14950614/working-of-asm-volatile-memory |
Clang can't win llvm)) |
I think gcc can win, but... Clang has unreal optimization in this case |
I don't make cross-language benches anymore(( |
That's what I'm talking about, but in Rust
The text was updated successfully, but these errors were encountered: