Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70310

Re: enum type in template declaration

From Soviet_Mario <SovietMario@CCCP.MIR>
Newsgroups comp.lang.c++
Subject Re: enum type in template declaration
Date 2019-12-06 17:20 +0100
Organization A noiseless patient Spider
Message-ID <qsdv4a$fa2$1@dont-email.me> (permalink)
References <qsbvs3$81l$1@dont-email.me> <qscii5$5an$1@dont-email.me> <qsdgou$paa$1@dont-email.me> <qsdib1$2j6$1@dont-email.me>

Show all headers | View raw


Il 06/12/19 13:42, Paavo Helde ha scritto:
> On 6.12.2019 14:15, Soviet_Mario wrote:
>> Il 06/12/19 04:39, James Kuyper ha scritto:
>>> On 12/5/19 5:20 PM, Soviet_Mario wrote:
>>>> since what version of standard, if any, an ENUM type can be
>>>> an argument of a template ?
>>>
>>> It has always been possible to use an enumerated type as 
>>> a template
>>> parameter.
>>
>> sorry I'm not sure I have understood
> 
> A template parameter can be a type, an int, etc. An enum 
> type is also a type:
> 
> enum RestrictedValues { A = 1, B = 2, C = 4, D = 8 };
> 
> template<typename T>
> class A {
>    T member;
> };
> 
> A<RestrictedValues> x; // OK
> 
> Here, an enum type is used as an argument of a template, as 
> you requested.
> 
> However, I suspect that you actually want to use an enum 
> value (not enum type) as the template parameter - see below.

none of both : I would like to declare that a parameter must 
be of enum type, so that the values actually received by the 
actual instance are not simply generic integral, but a 
selected subset.

Assume you would like to model a "pixel" with a variable 
depth of color, but that such depth should be checked (at 
compile time) to be a power of two, and set up an enum like

enum EnumDepths { two = 2, four = 4, eight = 8 }

then the template should not take a simple integral 
paramenter, as one could instantiate it wrongly

template <3> class ClsBMPofPixels;

so the typename must be of EnumDepths type, and only after 
this compile time check, yes, then internally the VALUE 
passed is used practically

> 
>>
>> Mmmm, exactly the point I am misunderstanding. I believed 
>> to have been
>> able to establish a proper type-checking in that declaration.
>> How instead it should have been written ?
>> can you correct my example in order to non accept non 
>> specific-enum
>> integral instancing ?
> 
> You have not said what your problem is. If you got a 
> compiler error, then what was the code and what was the error ?

no, still just trying to figure out and think, not yet any 
practical code

> 
> What you wrote was basically correct syntax (after 
> commenting out "etc etc"). This compiles fine:
> 
> enum RestrictedValues { A = 1, B = 2, C = 4, D = 8 };
> 
> template <RestrictedValues ttVal> class SizedField
> {
> private:
>      unsigned char zzArray[ttVal];
> 
> 
> public:
> 
>      //etc etc
> 
> };
> 
> int main() {
>      SizedField<B> foo;
> }
> 

yes but would such code protect me from writing


template <5> class SizedField; ? Would the compiler check 
that 5 is not a legal value of the enum ?

If so, okay ...


Oh, but BTW, what "helper" functions about run-time 
INTROSPECTION are more or less standard ?


I've found some is_enum<EnumType> (), is_array<typename> () 
and so.

But a simple function like : bool is_of_type <typename> ()

in other languages such operators are named like "TypeOf" 
(or typename, also a string representation could be usefull)

This question is for a concrete case.
I'm  trying to build a 2D table container not particularly 
generic : it supports just two type of contained items, 
either double either a "fixed size string", but I'm not sure 
I'd be able to write really generic functions as the two 
basic types contain some equivalent operators, constructors 
and comparison op, but also some semantically different 
accessors for higher level function on a container.

So I'd appreciate some way a container could look inside 
himself (at runtime) and be aware of its type and call 
proper specialized functions where the syntax are not 
equivalent and I cannot rely only on overload for automatic 
selection of the best fit, or even in case I have to choose 
to MIX data, thing that will end in ambiguity, as the two 
types (fixedstrings and double) are largely convertible and 
interchangeable, so a generic function with identical 
signatures could not know what is the best to be called)








-- 
1) Resistere, resistere, resistere.
2) Se tutti pagano le tasse, le tasse le pagano tutti
Soviet_Mario - (aka Gatto_Vizzato)

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

enum type in template declaration Soviet_Mario <SovietMario@CCCP.MIR> - 2019-12-05 23:20 +0100
  Re: enum type in template declaration James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-12-05 22:39 -0500
    Re: enum type in template declaration Soviet_Mario <SovietMario@CCCP.MIR> - 2019-12-06 13:15 +0100
      Re: enum type in template declaration Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-06 14:42 +0200
        Re: enum type in template declaration Soviet_Mario <SovietMario@CCCP.MIR> - 2019-12-06 17:20 +0100
          Re: enum type in template declaration Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-06 20:29 +0200
            Re: enum type in template declaration Soviet_Mario <SovietMario@CCCP.MIR> - 2019-12-06 22:42 +0100
              Re: enum type in template declaration Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-07 00:51 +0200
                Re: enum type in template declaration Soviet_Mario <SovietMario@CCCP.MIR> - 2019-12-07 18:54 +0100
      Re: enum type in template declaration Öö Tiib <ootiib@hot.ee> - 2019-12-06 06:00 -0800
      Re: enum type in template declaration James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-12-06 09:56 -0500
      Re: enum type in template declaration Manfred <noname@add.invalid> - 2019-12-06 15:56 +0100
  Re: enum type in template declaration Soviet_Mario <SovietMario@CCCP.MIR> - 2019-12-06 18:43 +0100
  Re: enum type in template declaration saturnman2008@gmail.com - 2019-12-07 01:53 -0800
    Re: enum type in template declaration Soviet_Mario <SovietMario@CCCP.MIR> - 2019-12-07 18:57 +0100

csiph-web