On Wed, Sep 30, 2009 at 4:14 PM, Markus Moll
<markus.moll@xxxxxxxxxxxxxxxx> wrote:
Hi
Another alternative is having a templated constructor which is a better match
for types other than T:
template<typename T, [...]>
class Replicate
{
[...]
template<typename U> struct dummy;
Replicate(const T& t);
template<typename U> Replicate(const U& u) { dummy<U>::template
ConstructionOnlyPossibleFromType<T>; }
[...]
};
int main()
{
SomeClass obj;
Replicate<SomeOtherClass> r( obj );
}
Here, gcc emits an error like:
: In constructor ‘X<T>::X(const U&) [with U = SomeClass, T = SomeOtherClass]’:
: instantiated from here
: error: ‘ConstructionOnlyPossibleFrom<T>’ is not a member of
‘X<SomeOtherClass>::dummy<SomeClass>’
Does anyone see any problem with either of these approaches?
If not, I guess it's a matter of taste.
Personally, I think the solution posted earlier is cleaner.