I am compiling the following code
<code>
template < unsigned int I >
class A {};
template < int I >
class B {};
template < int I >
B<-I> foo(A<I> const &)
{
return B<-I>();
}
template < int I >
void bar(A<I> const &)
{
}
int main()
{
A<1> a;
foo(a); // warning here
bar(a);
return 0;
}
</code>
At the indicated line, Visual Studio 2009 emits the following warning:
warning C4146: unary minus operator applied to unsigned type, result
still unsigned
I don't really understand the warning. It seems to me that the problem
could be the unsigned int to int conversion of the template argument
-- once it has been converted to int, appling the unary minus operator
should be a problem. But I get this warning for foo and no warning for
bar. What is the reason behind this?
More importantly, I would like to know if there would be any work-
around to avoid the warning.
Thank you,
Paul
I forgot to add that I don't wish to ban W4146 globally, and that a
pragma push/disable 4148/pop around foo does not work -- the warning
really is emitted at the line I indicated. I guess I could use these
around foo everytime I use it, or define a macro to do so, but I am
hoping for a more insightful solution.
"user790" <use...@gmail.com> wrote in message
news:1f986a40-dee4-46c1...@t13g2000yqt.googlegroups.com...
Ah, but it isn't converted to int until after the negation operator is
applied. Hence the problem, and the solution.
Right now you have
B<-I>
which is effectively
B<(int)(-I)>
Instead use
B<-(int)I>
Thanks for your response. However, I can't quite get your solution to
work. Replacing foo with
template < int I >
B<-(int)I> foo(A<I> const &)
{
return B<-(int)I>();
}
yields
error C2893: Failed to specialize function template 'B<-I> foo(const
A<I> &)'
at the line where I previously had a warning.
"user790" <use...@gmail.com> wrote in message
news:e0680321-be83-4a5b...@p29g2000yqh.googlegroups.com...
Do you have a prototype or other definition of foo anywhere? I don't see
why it should be trying to specialize anything.
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 4236 (20090712) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>
__________ Information from ESET NOD32 Antivirus, version of virus signature database 4236 (20090712) __________
The message was checked by ESET NOD32 Antivirus.
Absolutely not. If you copy-paste the code as it is, without any
includes, and compile it, you should get the same error as I have
(with Visual 2008 anyway).