Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Integer template argument warning

0 views
Skip to first unread message

user790

unread,
Jul 6, 2009, 1:01:18 PM7/6/09
to
Hello,

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

user790

unread,
Jul 6, 2009, 1:13:18 PM7/6/09
to
On Jul 6, 7:01 pm, user790 <user...@gmail.com> wrote:
> More importantly, I would like to know if there would be any work-
> around to avoid the warning.

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.

Ben Voigt [C++ MVP]

unread,
Jul 6, 2009, 3:37:21 PM7/6/09
to


"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>

user790

unread,
Jul 7, 2009, 3:20:25 AM7/7/09
to
On Jul 6, 9:37 pm, "Ben Voigt [C++ MVP]" <r...@newsgroups.nospam>
wrote:

> 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.

Ben Voigt [C++ MVP]

unread,
Jul 12, 2009, 11:52:47 AM7/12/09
to

"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.

http://www.eset.com

user790

unread,
Jul 15, 2009, 3:15:42 AM7/15/09
to
On Jul 12, 5:52 pm, "Ben Voigt [C++ MVP]" <bvo...@newsgroup.nospam>
wrote:

> Do you have a prototype or other definition of foo anywhere?  I don't see
> why it should be trying to specialize anything.

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).

0 new messages