Skip to content

Commit 4f68962

Browse files
committed
Allow using a type as both a base type and a member type, closes #333
`store_as_base<Name, T>` should hold its `T` by value, not by private base... I should know better than use private inheritance :) While I was trying an alternate solution (that I then backed out), I created a `cpp2::contains` function... now it isn't being used, but will likely be useful so I'll leave it in
1 parent aa1411f commit 4f68962

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

include/cpp2util.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,17 @@ class out {
641641

642642

643643
template<String Name, typename T>
644-
struct store_as_base : private T
644+
class store_as_base
645645
{
646-
store_as_base( T const& t ) : T{t} { }
647-
store_as_base( T && t ) : T{std::move(t)} { }
648-
store_as_base( auto && args ) : T{CPP2_FORWARD(args)} { }
646+
T value;
649647

650-
auto value__() -> T & { return *this; }
651-
auto value__() const -> T const& { return *this; }
648+
public:
649+
store_as_base( T const& t ) : value{t} { }
650+
store_as_base( T && t ) : value{std::move(t)} { }
651+
store_as_base( auto && args ) : value{CPP2_FORWARD(args)} { }
652+
653+
auto value__() -> T & { return value; }
654+
auto value__() const -> T const& { return value; }
652655
};
653656

654657

regression-tests/test-results/pure2-ufcs-member-access-and-chaining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extern int y;
4747

4848
CPP2_UFCS_0(ufcs, get_i(fun()));
4949

50-
auto res {CPP2_UFCS_0(ufcs, 42)};
50+
auto res {CPP2_UFCS_0(ufcs, (42))};
5151

5252
CPP2_UFCS_0(ufcs, (std::move(j).i));
5353
}

source/common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,21 @@ auto starts_with(
487487
}
488488

489489

490+
auto contains(
491+
auto const& range,
492+
auto const& value
493+
)
494+
-> bool
495+
{
496+
return std::find(
497+
range.begin(),
498+
range.end(),
499+
value
500+
)
501+
!= range.end();
502+
}
503+
504+
490505
//-----------------------------------------------------------------------
491506
//
492507
// Command line handling

0 commit comments

Comments
 (0)