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

Re: Define a function if not declared?

31 views
Skip to first unread message
Message has been deleted

Victor Bazarov

unread,
Apr 19, 2015, 8:37:25 PM4/19/15
to
On 4/19/2015 7:30 PM, Stefan Ram wrote:
> Can one define a function only in the case that a function
> is not declared?
>
> For example, define »make_unique« at the top of a
> translation unit with lines that only have this effect
> when make_unique is not already declared at this point?

There is probably a way to do it using SFINAE, by trying to take the
address of that function. If the function is not declared, taking the
address of it should be not possible, so the template is not going to be
generated... Then you could try to define your function as the member
of that template, or specialize it... Seems like a lot of work for no
obvious gain, though.

What problem are you trying to solve?

V
--
I do not respond to top-posted replies, please don't ask
Message has been deleted

Louis Krupp

unread,
Apr 19, 2015, 9:56:45 PM4/19/15
to
On 19 Apr 2015 23:30:54 GMT, r...@zedat.fu-berlin.de (Stefan Ram)
wrote:

> Can one define a function only in the case that a function
> is not declared?
>
> For example, define »make_unique« at the top of a
> translation unit with lines that only have this effect
> when make_unique is not already declared at this point?

If you can depend on only C++14 implementations having make_unique,
then this page might be useful:

http://sourceforge.net/p/predef/wiki/Standards/

Louis

Paavo Helde

unread,
Apr 20, 2015, 1:46:45 AM4/20/15
to
r...@zedat.fu-berlin.de (Stefan Ram) wrote in news:make_unique-
2015042...@ram.dialup.fu-berlin.de:

> Victor Bazarov <v.ba...@comcast.invalid> writes:
>>On 4/19/2015 7:30 PM, Stefan Ram wrote:
>>>Can one define a function only in the case that a function
>>>is not declared?
> ...
>>What problem are you trying to solve?
>
> I want to write my code (e.g. for my tutorial) already in
> the C++14 style, e.g., using »::std::make_unique« instead
> of »::std::unique( new ...«.
>
> Some implementations do not already supply »make_unique«,
> so I thought about adding some lines at the top that define
> »make_unique« if it's not declared at this point.
>

This is typically done by a lot of preprocessor hackery. Here is a small
excerpt from such a hackery, meant for defining std::move when it is not
present. Such a mechanism provides a way to cope also with other mising
features, not only missing function definitions.

#ifdef __GNUC__
#define HAVE_COMPILER_GCC
#endif
// ...
#ifdef HAVE_COMPILER_GCC
#if __GNUC__>4 || (__GNUC__==4 && __GNUC_MINOR__>=3) // gcc 4.3
#define HAVE_C11_STATIC_ASSERT
// ...
#endif
#if __GNUC__>4 || (__GNUC__==4 && __GNUC_MINOR__>=4) // gcc 4.4
#define HAVE_C11_RVALUE_REFERENCES
// ...
#endif
#if __GNUC__>4 || (__GNUC__==4 && __GNUC_MINOR__>=5) // gcc 4.5
#define HAVE_C11_LAMBDA
// ...
#endif
// ...
#endif
// ...

#ifndef HAVE_C11_RVALUE_REFERENCES
// provide dummy std::move() for pre-c++1x compilers
// which do not have one.
namespace std {
template<class T> T move(const T& x) {return x;}
}
#endif

0 new messages