Anon
Anon

Reputation: 69

#define with :: (double colon) in identifier for namespaces

How can I #define a type that has a double colon in C++?

For example, I want to include the std::optional type from the std library. The code bellow won't work because the compiler cannot parse the double colon in #define my_namespace::optional.

#if __cplusplus >= 201703L // C++17
#include <optional>
#define my_namespace::optional std::optional
#else
#include <my_optional.h>
#define my_namespace::optional my_namespace::my_optional
#endif

// Use the optional type
my_namespace::optional<int32_t> some_value;

Upvotes: 2

Views: 640

Answers (1)

Anon
Anon

Reputation: 69

The answer (thanks to Davislor):

#if __cplusplus >= 201703L // C++17
#include <optional>
namespace my_namespace
{
    template<class T>
    using optional = std::optional<T>;
}
#else
#include <my_optional.h>
namespace my_namespace
{
    template<class T>
    using optional = my_optional<T>;
}
#endif

Upvotes: 2

Related Questions