Ulrich Lauther <[EMAIL PROTECTED]> 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'
_______________________________________________
help-gplusplus mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-gplusplus