And it really works on your machine? yes.
Is the code you posted here correct or are any modification neccessary?
afaict, yes.
Which compiler and on which platform did you test it?
I used gcc on cygwin (don't have machine with me so don't know gcc
version).
Also tested it just now on linux boxen with gcc 4.2.2.
Comeau says it's OK as well but has some overflow warnings which are
to be expected since this is a case of find out when the overflow
happens.
Comeau C/C++ 4.3.10.1 (May 29 2008 09:37:15) for
ONLINE_EVALUATION_BETA1
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 4: warning: integer conversion resulted in a
change of sign
static const T newv = V << T(1);
^
detected during:
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=short,
V=(short)16384, W=(short)32767, X=(short)8192,
Y=(short)16383, Z=true]" at line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=short,
V=(short)8192, W=(short)16383, X=(short)4096,
Y=(short)8191, Z=true]" at line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=short,
V=(short)4096, W=(short)8191, X=(short)2048,
Y=(short)4095, Z=true]" at line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=short,
V=(short)2048, W=(short)4095, X=(short)1024,
Y=(short)2047, Z=true]" at line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=short,
V=(short)1024, W=(short)2047, X=(short)512,
Y=(short)1023, Z=true]" at line 7
[ 6 instantiation contexts not shown ]
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=short,
V=(short)8, W=(short)15, X=(short)4, Y=(short)7,
Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=short,
V=(short)4, W=(short)7, X=(short)2, Y=(short)3,
Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=short,
V=(short)2, W=(short)3, X=(short)1, Y=(short)1,
Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=short,
V=(short)1, W=(short)1, X=(short)0, Y=(short)0,
Z=true]"
at line 21
instantiation of class "maxof<T> [with T=short]" at line
31
"ComeauTest.c", line 4: warning: integer conversion resulted in a
change of sign
static const T newv = V << T(1);
^
detected during:
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=char,
V='@', W='\177', X=' ', Y='?', Z=true]" at line
7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=char,
V=' ', W='?', X='\020', Y='\037', Z=true]" at
line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=char,
V='\020', W='\037', X='\b', Y='\017', Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=char,
V='\b', W='\017', X='\004', Y='\a', Z=true]" at
line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=char,
V='\004', W='\a', X='\002', Y='\003', Z=true]"
at line 7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=char,
V='\002', W='\003', X='\001', Y='\001', Z=true]"
at line
7
instantiation of class "max_val<T, V, W, X, Y, Z> [with
T=char,
V='\001', W='\001', X='\000', Y='\000', Z=true]"
at line
21
instantiation of class "maxof<T> [with T=char]" at line 32