C++ iterators are absolute retarded shite. Their ```design""" makes no sense.

Fortune for rngxx's current commit: Future curse − 末凶
iter
Avril 3 years ago
parent b071cbb1ad
commit 3ff3db0e3e
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -59,6 +59,7 @@ struct Random
{ {
template<typename T> template<typename T>
struct iterator { //TODO: Implement this in another file (has to be header because of template :/) struct iterator { //TODO: Implement this in another file (has to be header because of template :/)
//XXX: C++ iterators are absolute jank. This is far harder than it should be.
friend class Random; friend class Random;
//TODO: Make this work with foreach(), and STL iterator APIs somehow //TODO: Make this work with foreach(), and STL iterator APIs somehow
@ -70,6 +71,15 @@ struct Random
inline virtual ~iterator(){} inline virtual ~iterator(){}
inline T next() { return _sample(); }
inline iterator& operator++() { return *this; }
inline iterator& operator++(int) { return *this; }
inline T operator*() { return next(); }
friend auto operator<=>(const iterator<T>&, const iterator<T>&) = default;
protected: protected:
virtual T _sample() { if (LIKELY(rng)) return rng->next<T>(); else throw ObjectMoved(); } virtual T _sample() { if (LIKELY(rng)) return rng->next<T>(); else throw ObjectMoved(); }
virtual inline void _init() {} virtual inline void _init() {}
@ -165,7 +175,7 @@ struct Random
inline T next(T max); inline T next(T max);
template<typename T> template<typename T>
inline iterator<T> iter() { return iterator(*this); } //TODO: An iterator that yields `next<T>()` forever. inline iterator<T> iter() { return iterator<T>(*this); } //TODO: An iterator that yields `next<T>()` forever.
template<typename T> template<typename T>
constexpr inline T max_for() const; constexpr inline T max_for() const;

@ -0,0 +1,10 @@
#include <rngxx.hpp>
//XXX: Doing this is not going to work ...
#if 0
void t(Random& r)
{
for(auto&& i : r.iter<int>()) (void)i;
}
#endif

@ -6,6 +6,7 @@
#define _RNGXX_INLINE_ONLY #define _RNGXX_INLINE_ONLY
#include <rngxx.h> #include <rngxx.h>
static int next(rng_t* rng, const int* min, const int* max) static int next(rng_t* rng, const int* min, const int* max)
{ {
int output; int output;
@ -68,5 +69,6 @@ int main()
printf("\n"); printf("\n");
rng_free(engine); rng_free(engine);
return 0; return 0;
} }

@ -0,0 +1,17 @@
#include <fmt/format.h>
#include <rngxx.hpp>
#include <rngxx/crand.h>
void iter(Random& rng)
{
for(auto&& i : rng.iter<int>())
fmt::print("{} ", i);
}
extern "C" {
void cpp_test()
{
auto random = new rng::crand(100);
iter(random);
}
}
Loading…
Cancel
Save