Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global namespace scope operator on function definition

I am in the process of creating a C wrapper around a C++ library. One common mistake to make while doing this is having a function declaration and definition that do not match for some reason (typo, renames, argument got added/removed, etc).

For example:

// enabledata.h
MDS_C_API const char* motek_mds_enable_data_get_enable_command_name();

// enabledata.cpp
const char* motek_mds_enable_data_enable_command_name() { ... }

The names do not match, but because of the lack of scope for these functions, it will not result in any compile errors, and will only show up much later down the line as a link error.

I want the compiler to help me find these errors by using the global scope operator like so:

const char* ::motek_mds_enable_data_get_disable_command_name() { ... }

This will now show up as a compile error if the function has not been declared yet, which is exactly what I want.

However, this does not work when the function returns a typedef:

int32_t ::motek_mds_enable_data_is_enabled(const Data* a_Data) { ... }

This will result in an attempt to use int32_t as a scope, which of course results in an error:

left of '::' must be a class/struct/union

Are there any ways to make this work? Better alternatives are also welcome of course.

I am currently using Visual Studio 2015 Update 2.

like image 735
Rick de Water Avatar asked Apr 14 '16 09:04

Rick de Water


1 Answers

You can always parenthesize the declarator-id:

int32_t (::motek_mds_enable_data_is_enabled)(const Data* a_Data) { ... }
//      ^                                  ^
like image 68
T.C. Avatar answered Sep 30 '22 19:09

T.C.