Skip to content
Snippets Groups Projects
Commit 3c2d0b9a authored by Carsten Gräser's avatar Carsten Gräser
Browse files

Add a wrapper storing either a copy or reference

parent 8b4a4472
No related branches found
No related tags found
No related merge requests found
Pipeline #
...@@ -3,6 +3,7 @@ install(FILES ...@@ -3,6 +3,7 @@ install(FILES
arithmetic.hh arithmetic.hh
boxconstraint.hh boxconstraint.hh
canignore.hh canignore.hh
copyorreference.hh
defaultbitvector.hh defaultbitvector.hh
genericvectortools.hh genericvectortools.hh
interval.hh interval.hh
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_SOLVERS_COMMON_COPYORREFERENCE_HH
#define DUNE_SOLVERS_COMMON_COPYORREFERENCE_HH
#include <dune/common/indices.hh>
#include <dune/common/concept.hh>
#include <dune/solvers/common/defaultbitvector.hh>
namespace Dune {
namespace Solvers {
/**
* \brief A wrapper storing either a copy or reference
*
* \tparam T Type to be refered to
*
* If T is of reference type, the wrapper stores a reference
* to the passed object. Otherwise it stores a copy.
*/
template<class T>
class CopyOrReference
{
public:
using Type = std::decay_t<T>;
constexpr CopyOrReference(const Type& other) :
t_(other)
{}
constexpr CopyOrReference(Type&& other) :
t_(std::move(other))
{}
constexpr const Type& get() const
{
return t_;
}
constexpr Type& get()
{
return t_;
}
constexpr std::true_type hasCopy() const
{
return {};
}
private:
Type t_;
};
template<class T>
class CopyOrReference<T&>
{
public:
using Type = std::decay_t<T>;
constexpr CopyOrReference(Type& other) :
t_(&other)
{}
constexpr const Type& get() const
{
return *t_;
}
constexpr Type& get()
{
return *t_;
}
constexpr std::false_type hasCopy() const
{
return {};
}
private:
Type* t_;
};
template<class T>
class CopyOrReference<const T&>
{
public:
using Type = std::decay_t<T>;
constexpr CopyOrReference(const Type& other) :
t_(&other)
{}
constexpr const Type& get() const
{
return *t_;
}
constexpr std::false_type hasCopy() const
{
return {};
}
private:
const Type* t_;
};
} // end namespace Solvers
} // end namespace Dune
#endif // DUNE_SOLVERS_COMMON_COPYORREFERENCE_HH
  • Developer

    For GCC 4.9.2.:

    In line 39 and following (and, analogously, in line 70 and following) I get errors that these methods (constexpr const Type& get() const and constexpr Type& get()) cannot be overloaded. Dropping constexpr resolves this issue, though I'm not quite sure whether that's what you intended.

  • Owner

    The constexpr is intended and correct C++14. We want the const and the non-const method to be constexpr. However, in C++11 constexpr implies const hence both methods would be const which is not allowed. While we require C++14 which makes the code correct, this language change is not implemented for some of the older supported compilers. Hence the proper workaround is to drop the constexpr for the non-const methods.

  • maxka @maxka

    mentioned in issue #9

    ·

    mentioned in issue #9

    Toggle commit list
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment