Comments (3)
what do you think about defining your own literal operator in your codebase?
I think it is the reasonable approach, since I tested to override more functions, and of course I ran into ambiguities (see below for more details if you are interested).
Until C++23 will save the day
better late than never ;-)
More details when trying to override more functions. I took the quick and (not so) dirty macro way:
#define add_int_override_sizet_container(fn) \
template <typename Container, \
typename ContainerOut = internal::remove_const_and_ref_t<Container>> \
ContainerOut fn(int amount, Container&& xs) \
{ \
assert(amount >= 0); \
size_t amount_s = static_cast<size_t>(amount); \
return fn(amount_s, xs); \
}
add_int_override_sizet_container(drop);
add_int_override_sizet_container(take);
add_int_override_sizet_container(repeat);
add_int_override_sizet_container(stride);
add_int_override_sizet_container(shuffle);
add_int_override_sizet_container(drop_idx);
add_int_override_sizet_container(drop_last);
add_int_override_sizet_container(take_last);
add_int_override_sizet_container(drop_exact);
add_int_override_sizet_container(take_exact);
add_int_override_sizet_container(take_cyclic);
add_int_override_sizet_container(partial_sort);
add_int_override_sizet_container(replicate_elems);
...
And as expected, I got quite a lot of errors when compiling container_common_test.cpp
(ambiguous calls and conversion errors).
...
include/fplus/container_common.hpp:2285:1: error: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Werror,-Wshorten-64-to-32]
add_int_override_sizet_container(repeat);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fplus/include/fplus/container_common.hpp:2280:15: note: expanded from macro 'add_int_override_sizet_container'
return fn(amount_s, xs); \
~~ ^~~~~~~~
fplus/test/container_common_test.cpp:238:16: note: in instantiation of function template specialization 'fplus::repeat<std::vector<int> &, std::vector<int>>' requested here
REQUIRE_EQ(repeat(2, xs), xs2Times);
^
In file included from fplus/test/container_common_test.cpp:8:
In file included from fplus/include/fplus/fplus.hpp:11:
fplus/include/fplus/container_common.hpp:2293:34: error: call to 'take_cyclic' is ambiguous
add_int_override_sizet_container(take_cyclic);
^~~~~~~~~~~
fplus/include/fplus/container_common.hpp:2280:12: note: expanded from macro 'add_int_override_sizet_container'
return fn(amount_s, xs); \
^~
fplus/test/container_common_test.cpp:612:16: note: in instantiation of function template specialization 'fplus::take_cyclic<std::vector<int>, std::vector<int>>' requested here
REQUIRE_EQ(take_cyclic(5, IntVector({0,1,2,3})), IntVector({0,1,2,3,0}));
^
fplus/include/fplus/container_common.hpp:766:11: note: candidate function [with Container = std::vector<int>]
Container take_cyclic(std::size_t amount, const Container& xs)
^
fplus/include/fplus/container_common.hpp:2293:34: note: candidate function [with Container = std::vector<int> &, ContainerOut = std::vector<int>]
add_int_override_sizet_container(take_cyclic);
^
fplus/include/fplus/container_common.hpp:2286:1: error: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Werror,-Wshorten-64-to-32]
add_int_override_sizet_container(stride);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fplus/include/fplus/container_common.hpp:2280:15: note: expanded from macro 'add_int_override_sizet_container'
return fn(amount_s, xs); \
~~ ^~~~~~~~
fplus/test/container_common_test.cpp:731:16: note: in instantiation of function template specialization 'fplus::stride<std::vector<int>, std::vector<int>>' requested here
REQUIRE_EQ(stride(3, IntVector({0,1,2,3,4,5,6,7})), IntVector({0,3,6}));
^
In file included from fplus/test/container_common_test.cpp:8:
In file included from fplus/include/fplus/fplus.hpp:11:
fplus/include/fplus/container_common.hpp:2286:1: error: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Werror,-Wshorten-64-to-32]
add_int_override_sizet_container(stride);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fplus/include/fplus/container_common.hpp:2280:15: note: expanded from macro 'add_int_override_sizet_container'
return fn(amount_s, xs); \
~~ ^~~~~~~~
fplus/include/fplus/container_common.hpp:2286:34: note: in instantiation of function template specialization 'fplus::stride<std::vector<int> &, std::vector<int>>' requested here
add_int_override_sizet_container(stride);
^
fplus/test/container_common_test.cpp:731:16: note: in instantiation of function template specialization 'fplus::stride<std::vector<int>, std::vector<int>>' requested here
REQUIRE_EQ(stride(3, IntVector({0,1,2,3,4,5,6,7})), IntVector({0,3,6}));
^
4 errors generated.
make[2]: *** [test/CMakeFiles/container_common_test.dir/container_common_test.cpp.o] Error 1
make[1]: *** [test/CMakeFiles/container_common_test.dir/all] Error 2
make: *** [all] Error 2
I think we can close this issue!
from functionalplus.
Oh, interesting. And good investigation. π
So far, I tried to avoid overloaded functions, e.g., to make it easier to pass them around as arguments (no ambiguity for a certain function name, etc.).
But of course, I see the ugliness of the explicit cast.
Until C++23 will save the day, what do you think about defining your own literal operator in your codebase?
from functionalplus.
Yeah, custom literal operators seem much better compared to going through function-overloading hell. π
from functionalplus.
Related Issues (20)
- error: exception handling disabled, use β-fexceptionsβ to enable HOT 1
- keep_by_idx() returns list of empty elements. HOT 4
- Branch `master` will be renamed to `main` in November HOT 1
- 0.2.16-p0: Tests fail to build: "EQUAL" "1" Unknown arguments specified HOT 10
- Cannot compile with gcc 11.2 in C++20 mode HOT 3
- make c++ class function to c pointer HOT 7
- fplus::compose with yet unknown output type HOT 3
- parallel_for ? (no, this is not the 1st of April ;-) HOT 3
- IO Monad HOT 5
- composition.hpp uses std::result_of HOT 5
- Function to print std::tuple HOT 5
- Some List Functions (in particular, fplus::transform etc.) do not work with custom container. What needs to be added? HOT 6
- Clang 16: incompatible initializer of type (container_common_test) HOT 3
- FunctionalPlus does not compile with C++20 (std::result_of was removed) HOT 3
- CI fails because of missing GLIBC HOT 8
- Conan packages of this library are often outdated HOT 1
- get for `variant` HOT 5
- fwd bind count for `just_with_default` HOT 1
- Methods directly on containers (e.g `maybe`) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from functionalplus.