Integer template argument warning

U

user790

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
 
U

user790

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

Ben Voigt [C++ MVP]

user790 said:
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?

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
 
U

user790

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

Ben Voigt [C++ MVP]

user790 said:
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.

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
 
U

user790

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top