[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: what's the difference between (size_t)n and size_t(n)? Bug?
From: |
Andre Poenitz |
Subject: |
Re: what's the difference between (size_t)n and size_t(n)? Bug? |
Date: |
Wed, 29 Oct 2008 23:10:52 +0100 |
User-agent: |
tin/1.9.2-20070201 ("Dalaruan") (UNIX) (Linux/2.6.24-19-generic (i686)) |
Ulrich Lauther <ulrich.lauther@siemens.com> wrote:
>
> This code:
>
> #include <stdio.h>
>
> class Foo {
> size_t m_n;
> public:
> inline explicit Foo(size_t n) : m_n(n) {
> }
> inline int operator()(int i) {
> return i;
> }
> }; // Foo
>
> int main() {
>
> int n = 10;
> Foo my_foo(size_t(n));
> // Foo my_foo((size_t)n);
>
> int i = 1;
> i += my_foo(i);
> printf("%d\n",i);
> return 0;
> }
>
> Gives:
>
> test.C: In function 'int main()':
> test.C:20: error: no match for 'operator+=' in 'i += my_foo(((size_t)i))'
> test.C:15: warning: unused variable 'n'
Correctly so, even if unexpected.
Foo my_foo(size_t(n));
can be read as a function declaration for a function 'my_foo', returning
a 'Foo' and taking a size_t argument. The parantheses around 'n' do not
change it.
And as the thing can be legally read as a function declaration, it is
one, causing the compiler later to barf on i+=... .
So to make the compiler parse it as intended, you need to use something
that's not readable as function declaration, e.g.
Foo my_foo((size_t(n)));
or
Foo my_foo = Foo(size_t(n));
or
Foo my_foo(size_t(n) + 0);
or something similarily ugly.
> When I use the commented out line, it works as expected.
That should work, too, indeed.
Regards,
Andre'