restricted templates, enum

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Stippler

    restricted templates, enum

    Hello,

    some time ago I learnt much about restricting template parameters by
    checking some properties upon these and exploiting SFINAE. But the
    existence of unnamed enum types makes me doubt if this approach can be
    helpful in a general project. An example:

    I have an operator/(), which I would like to overload for vector/T, where T
    is long, double, .... I want to be able to use any type, which is
    convertible to vector, so I use SFINAE and write something sematically
    like:

    template <class A, typename B>
    typename restrictTo<(isC onvertible<A, vector>::value &&
    isNumericalType <B>::value), vector>::type
    operator/(const A &a, const B &b){ ... }

    Soemthing like that works fine.
    But as soon as you have an unnamed enum anywhere in your code and a value of
    this enum is parameter to "/", you have lost - the compiler emits an error.
    For example, the STL implementation of the Intel C++-compiler has such
    code.
    Why an error?
    You would want to have builtin operator/() chosen for the enum. But the
    template version is actually taken - due to overload resolution. And here
    the error takes place: the compiler tries to instantiate a template
    parameter with an unnamed type -> error.
    And this will happen, as soon as you have some 'restricted template'
    operator/(). So forget restricted templates for operators or what? We can't
    influence, if users make use of unnamed enums. Is there a way to get around
    this trouble? A short example program, which causes the error (on como):

    enum { red, green, blue };

    template <typename T, typename S>
    int
    operator+(const T &, const S &)
    {
    return 0;
    }

    int
    main()
    {
    int i = 1;
    i = i+green;

    return 0;
    }

    regards,
    alex
  • Victor Bazarov

    #2
    Re: restricted templates, enum

    "Alexander Stippler" <stip@mathemati k.uni-ulm.de> wrote...[color=blue]
    > some time ago I learnt much about restricting template parameters by
    > checking some properties upon these and exploiting SFINAE. But the
    > existence of unnamed enum types makes me doubt if this approach can be
    > helpful in a general project. An example:
    >
    > I have an operator/(), which I would like to overload for vector/T, where[/color]
    T[color=blue]
    > is long, double, .... I want to be able to use any type, which is
    > convertible to vector, so I use SFINAE and write something sematically
    > like:
    >
    > template <class A, typename B>
    > typename restrictTo<(isC onvertible<A, vector>::value &&
    > isNumericalType <B>::value), vector>::type
    > operator/(const A &a, const B &b){ ... }
    >
    > Soemthing like that works fine.
    > But as soon as you have an unnamed enum anywhere in your code and a value[/color]
    of[color=blue]
    > this enum is parameter to "/", you have lost - the compiler emits an[/color]
    error.[color=blue]
    > For example, the STL implementation of the Intel C++-compiler has such
    > code.
    > Why an error?
    > You would want to have builtin operator/() chosen for the enum. But the
    > template version is actually taken - due to overload resolution. And here
    > the error takes place: the compiler tries to instantiate a template
    > parameter with an unnamed type -> error.
    > And this will happen, as soon as you have some 'restricted template'
    > operator/(). So forget restricted templates for operators or what? We[/color]
    can't[color=blue]
    > influence, if users make use of unnamed enums. Is there a way to get[/color]
    around[color=blue]
    > this trouble?[/color]

    I think you answered your own question: don't use unnamed enums.
    As soon as you give a name to the enum, the code compiles fine.

    The cause is probably as simple as 14.3.1/2. An unnamed type shall
    not be used as a template argument for a type-parameter.
    [color=blue]
    > A short example program, which causes the error (on como):
    >
    > enum { red, green, blue };
    >
    > template <typename T, typename S>
    > int
    > operator+(const T &, const S &)
    > {
    > return 0;
    > }
    >
    > int
    > main()
    > {
    > int i = 1;
    > i = i+green;
    >
    > return 0;
    > }
    >
    > regards,
    > alex[/color]


    Comment

    Working...