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

unary + to pointer

52 views
Skip to first unread message

Öö Tiib

unread,
Feb 19, 2016, 7:47:21 AM2/19/16
to
13.6 Built-in operators [over.built]
8 For every type T there exist candidate operator functions of the form T* operator+(T*);

I can find zero uses in my code-bases so I started to wonder if
anyone uses it and what is the common usage of it?

Alf P. Steinbach

unread,
Feb 19, 2016, 8:29:20 AM2/19/16
to
I don't think it's used at all for pointers, but it /can/ be used to
produce an rvalue, which e.g. might guide overload resolution.

For integral types the unary `+` is a nice way to force promotion.

However, writing `+ 0` is perhaps more clear.


Cheers & hth.,

- Alf

Marcel Mueller

unread,
Feb 19, 2016, 3:08:05 PM2/19/16
to
I remember some code fragments where I needed operator + to make the
code compile.
[...seeking...]
Ah, here it is:

ControlBase(+GetHwnd()).Text(title);

ControlBase is a constructor of a lightweight C++ wrapper around dialog
controls taking HWND as parameter. GetHwnd() returns HWND. .Text(...) is
a member function of ControlBase to send a window message to set the
window title.

Without operator+ gcc complains:

gui/dialog.cpp: In constructor `UrlDialog::UrlDialog(long unsigned int,
const char*)':
gui/dialog.cpp:177: error: parse error before `.' token



Marcel

Öö Tiib

unread,
Feb 20, 2016, 7:30:30 AM2/20/16
to
That is embarrassing but I can't see what is wrong without that +.
Are there some sort of macros or templates that screw it up somehow?

Alf P. Steinbach

unread,
Feb 20, 2016, 9:08:07 AM2/20/16
to
Most vexing parse.

Öö Tiib

unread,
Feb 20, 2016, 9:46:23 AM2/20/16
to
But how? Trying to manufacture some dummies here does not reveal the issue:

#include <iostream>
#include <string>

using HWND = int const*;

HWND GetHwnd() {return nullptr;}

struct ControlBase
{
explicit ControlBase(HWND) {}
void Text( std::string const& t) {std::cout << t << std::endl;}
};

int main()
{
std::string title {"works"};
// No + ... but compiler does parse.
ControlBase(GetHwnd()).Text(title);
}


Alf P. Steinbach

unread,
Feb 20, 2016, 11:01:57 AM2/20/16
to
This seems to be a gray area, because Visual C++ 2015 and g++ 5.1 differ
in their opinion of almost identical source code:

#include <iostream>
#include <stddef.h>
#include <string>

typedef int const* HWND;

HWND GetHwnd() {return NULL;}

struct ControlBase
{
explicit ControlBase(HWND) {}
void Text( std::string const& t) {std::cout << t << std::endl;}
};

int main()
{
std::string title = "works";
// No + ... g++ accepts it, but MSVC issues compilation error.
ControlBase(GetHwnd()); //.Text(title);
}

g++ 5.1.0 accepts it, while Visual C++ 2015 produces this compilation error:

> foo.cpp(19): error C2556: 'ControlBase GetHwnd(void)': overloaded function differs only by return type from 'HWND GetHwnd(void)'
> foo.cpp(7): note: see declaration of 'GetHwnd'
> foo.cpp(19): error C2040: 'GetHwnd': 'ControlBase (void)' differs in levels of indirection from 'HWND (void)'


This indicates to me that it's /possible/ to parse the first part as a
function declaration. Adding a unary `+` removes the MSVC error. And so
I conclude that the most vexing parse is involved for the modified code,
and somehow -- unspecified -- was the reason for original error.


Cheers & sorry for not having the whole story,

- Alf


0 new messages