Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include global function in namespace in C++

Tags:

c++

Is there a way to include global functions (say from a library where I'm not allowed to modify the code) in to a namespace's scope and still be able to use it?

I have two functions:

base64_decode()
base64_encode()

in two files: Base64.cpp Base64.h.

(Obviously) when including Base64.h in to my Extensions namespace, the function declarations are available, but the linker can't find the definitions (in Base64.cpp) because they're now included in my namespace. Example:

namespace Extensions {
    #include "Base64.h"
}

Is there a way to have both the implementation and the declaration of the two functions inside the namespace without modifying the original code and without #includeing Base64.cpp?

like image 348
SimonC Avatar asked Apr 20 '26 17:04

SimonC


1 Answers

One common way:

#include "Base64.h"

namespace Extensions {

using ::base64_decode;
using ::base64_encode;

}

static_assert(sizeof(&Extensions::base64_decode) > 0, "");
static_assert(sizeof(&Extensions::base64_encode) > 0, "");
like image 95
Maxim Egorushkin Avatar answered Apr 22 '26 05:04

Maxim Egorushkin