Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Core Modules
dune-common
Commits
1ab4d91f
Commit
1ab4d91f
authored
5 years ago
by
Carsten Gräser
Browse files
Options
Downloads
Patches
Plain Diff
Allow returning references in Std::visit()
parent
07b27021
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!664
Allow returning references in Std::visit()
Pipeline
#18289
passed
5 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dune/common/std/variant.hh
+14
-48
14 additions, 48 deletions
dune/common/std/variant.hh
dune/common/test/varianttest.cc
+9
-0
9 additions, 0 deletions
dune/common/test/varianttest.cc
with
23 additions
and
48 deletions
dune/common/std/variant.hh
+
14
−
48
View file @
1ab4d91f
...
...
@@ -338,55 +338,21 @@ namespace Impl {
* in this variant.
*/
template
<
typename
F
>
auto
visit
(
F
&&
func
)
{
using
namespace
Dune
::
Hybrid
;
using
Result
=
decltype
(
func
(
unions_
.
getByIndex
(
std
::
integral_constant
<
size_t
,
0
>
())));
return
ifElse
(
std
::
is_same
<
Result
,
void
>
(),
[
&
,
this
](
auto
id
)
{
constexpr
auto
tsize
=
size_
;
Dune
::
Hybrid
::
forEach
(
Dune
::
Hybrid
::
integralRange
(
std
::
integral_constant
<
size_t
,
tsize
>
()),
[
&
](
auto
i
)
{
if
(
i
==
this
->
index_
)
func
(
id
(
unions_
).
getByIndex
(
std
::
integral_constant
<
size_t
,
i
>
()));
});
return
;},
[
&
func
,
this
](
auto
id
)
{
constexpr
auto
tsize
=
size_
;
auto
result
=
std
::
unique_ptr
<
Result
>
();
Dune
::
Hybrid
::
forEach
(
Dune
::
Hybrid
::
integralRange
(
std
::
integral_constant
<
size_t
,
tsize
>
()),
[
&
,
this
](
auto
i
)
{
if
(
i
==
this
->
index_
)
result
=
std
::
make_unique
<
Result
>
(
func
(
id
(
this
->
unions_
).
getByIndex
(
std
::
integral_constant
<
size_t
,
i
>
())));
});
return
*
result
;
});
decltype
(
auto
)
visit
(
F
&&
func
)
{
auto
dummyElseBranch
=
[
&
]()
->
decltype
(
auto
)
{
return
func
(
this
->
get
<
0
>
());};
auto
indices
=
std
::
make_index_sequence
<
size_
>
{};
return
Hybrid
::
switchCases
(
indices
,
index
(),
[
&
](
auto
staticIndex
)
->
decltype
(
auto
)
{
return
func
(
this
->
template
get
<
decltype
(
staticIndex
)
::
value
>());
},
dummyElseBranch
);
}
template
<
typename
F
>
auto
visit
(
F
&&
func
)
const
{
using
namespace
Dune
::
Hybrid
;
using
Result
=
decltype
(
func
(
unions_
.
getByIndex
(
std
::
integral_constant
<
size_t
,
0
>
())));
return
ifElse
(
std
::
is_same
<
Result
,
void
>
(),
[
&
,
this
](
auto
id
)
{
constexpr
auto
tsize
=
size_
;
Dune
::
Hybrid
::
forEach
(
Dune
::
Hybrid
::
integralRange
(
std
::
integral_constant
<
size_t
,
tsize
>
()),
[
&
](
auto
i
)
{
if
(
i
==
this
->
index_
)
func
(
id
(
unions_
).
getByIndex
(
std
::
integral_constant
<
size_t
,
i
>
()));
});
return
;},
[
&
func
,
this
](
auto
id
)
{
constexpr
auto
tsize
=
size_
;
auto
result
=
std
::
unique_ptr
<
Result
>
();
Dune
::
Hybrid
::
forEach
(
Dune
::
Hybrid
::
integralRange
(
std
::
integral_constant
<
size_t
,
tsize
>
()),
[
&
,
this
](
auto
i
)
{
if
(
i
==
this
->
index_
)
result
=
std
::
make_unique
<
Result
>
(
func
(
id
(
this
->
unions_
).
getByIndex
(
std
::
integral_constant
<
size_t
,
i
>
())));
});
return
*
result
;
});
decltype
(
auto
)
visit
(
F
&&
func
)
const
{
auto
dummyElseBranch
=
[
&
]()
->
decltype
(
auto
)
{
return
func
(
this
->
get
<
0
>
());};
auto
indices
=
std
::
make_index_sequence
<
size_
>
{};
return
Hybrid
::
switchCases
(
indices
,
index
(),
[
&
](
auto
staticIndex
)
->
decltype
(
auto
)
{
return
func
(
this
->
template
get
<
decltype
(
staticIndex
)
::
value
>());
},
dummyElseBranch
);
}
/** \brief Check if a given type is the one that is currently active in the variant. */
...
...
@@ -428,12 +394,12 @@ namespace Impl {
}
template
<
typename
F
,
typename
...
T
>
auto
visit
(
F
&&
visitor
,
variant
<
T
...
>&
var
)
{
decltype
(
auto
)
visit
(
F
&&
visitor
,
variant
<
T
...
>&
var
)
{
return
var
.
visit
(
std
::
forward
<
F
>
(
visitor
));
}
template
<
typename
F
,
typename
...
T
>
auto
visit
(
F
&&
visitor
,
const
variant
<
T
...
>&
var
)
{
decltype
(
auto
)
visit
(
F
&&
visitor
,
const
variant
<
T
...
>&
var
)
{
return
var
.
visit
(
std
::
forward
<
F
>
(
visitor
));
}
...
...
This diff is collapsed.
Click to expand it.
dune/common/test/varianttest.cc
+
9
−
0
View file @
1ab4d91f
...
...
@@ -87,6 +87,15 @@ Dune::TestSuite testVariant() {
suite
.
check
(
Std
::
visit
(
size
,
constv2
)
==
2
,
"Test const visit"
);
suite
.
check
(
Std
::
get_if
<
V2
>
(
&
constv2
)
!=
nullptr
,
"Test const get_if"
);
// Check visitor returning lvalue
using
A
=
struct
{
int
j
;
int
i
;};
using
B
=
struct
{
int
i
;};
Std
::
variant
<
A
,
B
>
variant3
;
variant3
=
A
{
1
,
2
};
Std
::
visit
([](
auto
&&
value
)
->
decltype
(
auto
)
{
return
(
value
.
i
);
},
variant3
)
=
42
;
suite
.
check
(
Std
::
visit
([](
auto
&&
value
)
{
return
value
.
i
;},
variant3
)
==
42
,
"Std::visit returning lvalue"
);
/// test copy and move construction/assignment
{
auto
variant_copy_constructed
=
variant2
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment