Re: template class with non-type-param template c-tor
On 2012-02-08 04:10, TomaszMikolajczyk wrote:
Disadvantage: class CopyConstructible requirement.
I don't understand your rationale. Which type needs to satisfy the
CopyConstructible requirements? From your original Range template we can
already deduce that you need to impose the CopyConstructible
requirements on T (assuming T is an object type). Without this
requirement you could not construct your _min and _max members anyway.
TM: My apologize I was not clear and use a shorthand. I assumed
possibility that a real class used in the app. is uncopyable
(private copy ctor and assigment op.)
OK, that makes it clearer. With this additional constraint you might
want to consider the following approach:
template<class T, T Min, T Max>
struct Limits
{
static_assert(Min <= Max, "invalid range");
};
template<typename T>
class RangeNoCopy
{
public:
RangeNoCopy(const T& min, const T& max) : _min(min), _max(max)
{
assert(min <= max);
}
template<T Min, T Max>
RangeNoCopy(Limits<T, Min, Max>) : _min(Min), _max(Max)
{
}
RangeNoCopy(const RangeNoCopy&) = delete;
RangeNoCopy& operator=(const RangeNoCopy&) = delete;
const T& getMin() const { return _min; }
const T& getMax() const { return _max; }
private:
T _min, _max;
};
RangeNoCopy<int> r(Limits<int, 1, 0>{});
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]