inline namespace technique for managing platform specific code in c++ - c++

I have seen usage of #ifdef macros ( example Eigen library ) to manage platform specific, but haven't seen any one use "inline namespace"s to manage platform specific code.
The github repo belows gives specific code and example usage.
https://github.com/dchichkov/curious-namespace-trick/wiki/Curious-Namespace-Trick
I am wondering if it is a viable technique to use or if there are any gotchas that I am not able to see. Below is the code snippet :
#include <stdio.h>
namespace project {
// arm/math.h
namespace arm {
inline void add_() {printf("arm add\n");} // try comment out
}
// math.h
inline void add_() {
//
printf("common add\n");
//
} inline namespace platform {inline void add() {add_();}}
inline void dot_() {
//
add();
//
} inline namespace platform {inline void dot() {dot_();}}
}
int main() {
project::dot();
return 1;
}
Output :
$g++ func.cpp -Dplatform=common ; ./a.out
common add
$ g++ func.cpp -Dplatform=arm ; ./a.out
arm add

There are at least two ways to achieve same results. First one with namespaces. Second one - with static functions in classes.
With namespaces:
#include <stdio.h>
namespace GenericMath
{
void add();
void dot();
}
namespace ArmMath
{
void add();
using GenericMath::dot;
}
namespace GenericMath
{
void add()
{
printf("generic add");
}
void dot()
{
Math::add();
printf("generic dot");
}
}
namespace ArmMath
{
void add()
{
printf("arm add");
}
using GenericMath::dot;
}
int main()
{
Math::dot();
return 1;
}
With classes:
#include <stdio.h>
class GenericMath
{
public:
static void add();
static void dot();
};
class ArmMath : public GenericMath
{
public:
static void add();
};
void GenericMath::add()
{
printf("generic add");
}
void GenericMath::dot()
{
printf("generic dot");
Math::add();
}
void ArmMath::add()
{
printf("arm add");
}
int main()
{
Math::add();
Math::dot();
return 1;
}
IMO inline namespaces make code less readable and too verbose.

Once issue is that unlike #ifdef, the compiler still compiles all the code that isn't for the current platform.
So you can't use it for dealing with platform-specific APIs.

Related

C++ Class Methods Definition Syntax

In C++, it is sometimes considered good practice to declare your classes in a header file and define all the methods in a cpp file. I understand this, but a consequence of this seems to be that instead of having all of the class methods tabbed-in inside curly braces, they are just out in the open in the cpp file. Is there any way to group the methods of a class together in the cpp file while still declaring them in a header file? I like being able to collapse things in my IDE... I'd just get over it, but it's been a while since I've coded anything in C++ and I'm wondering if there's a way to do it that I just forgot about.
To be clear what I mean, here's an example:
test.h:
class Testing {
public:
Testing(int x);
void print();
int x;
};
test.cpp:
#include <iostream>
#include "test.h"
using namespace std;
// class Testing {
// public:
// Testing(int x){
// this->x = x;
// }
// void print(){
// cout << this->x << endl;
// }
// };
Testing::Testing(int x){
this-> x = x;
}
void Testing::print(){
cout << this->x;
}
int main(){
Testing t(100);
t.print();
}
I'd like to do what is commented above in test.cpp instead, but that doesn't work, right? (I think it'd be like declaring a new class distinct from the one in the header file?)
You could do this:
== h.h ==
namespace H_DEFS {
class H {
public:
int A();
int B();
};
}
using namespace H_DEFS;
== h.cpp file ==
#include "h.h"
namespace H_DEFS {
int H::A() { return 4;};
int H::B() { return 5;};
}
== main.cpp ==
#include "h.h"
int main() {
return H().A() + H().B();
}
but it's a weird idiom for other programmers to read just for the benefit of your IDE.

Class members have different values in different parts of code

I'm trying to implement a non-blocking serial communication in my C++ app. A thread is responsible to do serial communication, and I've written a ThreadSafeClass to exchange data between serial thread and main thread. Here is the core of my code:
main.cpp
#include "serial.hpp"
#include "tsqueue.hpp"
int main(int argc, char *argv[])
{
serial::init();
while (true)
{
fgets(s);
serial::outQueue.enqueue(std::string(s));
}
serial::shutdown();
return 0;
}
tsqueue.hpp
#include <mutex>
#include <queue>
namespace tsqueue
{
template <typename T>
class ThreadSafeQueue
{
private:
mutable std::mutex _mtx;
std::queue<T> _que;
public:
ThreadSafeQueue();
~ThreadSafeQueue();
void enqueue(const T &item);
T tryDequeue(const T &defaultValue, bool &done);
void clear();
bool isEmpty() const;
};
template <typename T>
ThreadSafeQueue<T>::ThreadSafeQueue() {}
template <typename T>
ThreadSafeQueue<T>::~ThreadSafeQueue() { clear(); }
template <typename T>
void tsqueue::ThreadSafeQueue<T>::enqueue(const T &item)
{
std::lock_guard<std::mutex> lock(_mtx);
_que.push(item);
}
template <typename T>
T tsqueue::ThreadSafeQueue<T>::tryDequeue(const T &defaultValue, bool &done)
{
std::lock_guard<std::mutex> lock(_mtx);
if (_que.empty())
{
done = false;
return defaultValue;
}
else
{
T item = _que.front();
_que.pop();
done = true;
return item;
}
}
} // namespace tsqueue
And serial declaration/definition,
serial.hpp
#include <string>
#include "tsqueue.hpp"
namespace serial
{
static tsqueue::ThreadSafeQueue<std::string> inQueue;
static tsqueue::ThreadSafeQueue<std::string> outQueue;
void init();
void shutdown();
}
serial.cpp
#include <string>
#include "serial.hpp"
#include "tsqueue.hpp"
static std::thread _thread;
void run()
{
while (true)
{
std::string str = serial::outQueue.tryDequeue(emptyStr, dequeued);
if (dequeued) { /* Do send 'str' */ }
if (terminationRequested) { break; }
// Some sleep
}
}
void serial::init()
{
serial::inQueue.clear();
serial::outQueue.clear();
_thread = std::thread(run);
}
void serial::shutdown()
{
if (_thread.joinable()) { _thread.join(); }
}
The problem is, when tryDequeue(...) is called by serial thread's run() in serial.cpp, it always sees empty outQueue. However while loop still sees outQueue in main.cpp with provided data, even at later times. I've find out that using debug tools of vscode. I'm new to C++, but experienced in other languages. What am I doing wrong in above code? Do run() and main() see different objects?
Compiler: g++ 7.3.0, Environment: Linux (Ubuntu 18.04)
Edit: If I remove static from definitions of inQueue and outQueue, I get multiple definition error by linker for both. Although I have appropriate include guards.
(Heavily edited after all the non-issues have been repaired and after I finally spotted what was the actual problem:)
The problem is that you have two completely separate instances of outQueue: One in main.o and one in serial.o (or .obj if you are on Windows). The problem is that you declare these as static in a header. That results in individual copies of this in every *.cpp/object which included this header.
Ideally outQueue would not be a global variable. Assuming it should be a global variable you can fix this like this:
serial.hpp
namespace serial
{
// This is a declaration of a global variable. It is harmless
// to include this everywhere.
extern tsqueue::ThreadSafeQueue<std::string> inQueue;
extern tsqueue::ThreadSafeQueue<std::string> outQueue;
}
serial.cpp
namespace serial
{
// This is the actual definition of the variables.
// Without this you get unresolved symbols during link time
// (but no error during compile time). If you have this in
// two *.cpp files you will get multiple definition linker
// errors (but no error at compile time). This must not be
// static because we want all other objects to see this as well.
tsqueue::ThreadSafeQueue<std::string> inQueue;
tsqueue::ThreadSafeQueue<std::string> outQueue;
}
The ThreadSafeQueue itself looks ok to me.

Include issue: 'multiple definition', 'first defined here'

I have three files:
main.cpp
MyClass.cpp
MyClass.hpp
I have a library header file, "testLib.hpp", that I want to include in MyClass.hpp so that I can have one of testLib's objects be a class attribute.
I include MyClass.hpp in MyClass.cpp and in main.cpp. When attempting to compile the project, I get the following errors
MyClass.cpp multiple definition of 'testLib::testLib::function1()
obj/Release/main.o:main.cpp first defined here
MyClass.cpp multiple definition of 'testLib::testLib::function2()
obj/Release/main.o:main.cpp first defined here
and so on.
Both main.cpp and MyClass.cpp include MyClass.hpp (which includes testLib.hpp). Judging by the error, it looks like MyClass.cpp is attempting to include the library functions after they've already been included by main.cpp. However, I have include guards present in MyClass.hpp so I don't understand how it's trying to include MyClass.hpp twice.
Here's the code:
MyClass.hpp
#ifndef THIS_HEADER_H
#define THIS_HEADER_H
#include <stdint.h>
#include <iostream>
#include "testLib/testLib.hpp"
class MyClass
{
public:
void test();
int foo;
private:
uint32_t bar;
//I want to include an object from the library as part of this class
//TestLib::Device device;
};
#endif
MyClass.cpp
#include <stdio.h>
#include "MyClass.hpp"
void MyClass::test()
{
}
main.cpp
#include <iostream>
#include "MyClass.hpp"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Any help would be greatly appreciated!
EDIT
I tried to hide the actual filenames to make the question more general and clear, but it seems like the problem might be resulting from 'testLib.hpp', which I did not write. That file is actually the following "sweep.hpp" file. I got the 'multiple definition of/first defined here' errors for each of the public functions in this file:
sweep.hpp
#ifndef SWEEP_DC649F4E94D3_HPP
#define SWEEP_DC649F4E94D3_HPP
/*
* C++ Wrapper around the low-level primitives.
* Automatically handles resource management.
*
* sweep::sweep - device to interact with
* sweep::scan - a full scan returned by the device
* sweep::sample - a single sample in a full scan
*
* On error sweep::device_error gets thrown.
*/
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <vector>
#include <sweep/sweep.h>
namespace sweep {
// Error reporting
struct device_error final : std::runtime_error {
using base = std::runtime_error;
using base::base;
};
// Interface
struct sample {
const std::int32_t angle;
const std::int32_t distance;
const std::int32_t signal_strength;
};
struct scan {
std::vector<sample> samples;
};
class sweep {
public:
sweep(const char* port);
sweep(const char* port, std::int32_t bitrate);
void start_scanning();
void stop_scanning();
bool get_motor_ready();
std::int32_t get_motor_speed();
void set_motor_speed(std::int32_t speed);
std::int32_t get_sample_rate();
void set_sample_rate(std::int32_t speed);
scan get_scan();
void reset();
private:
std::unique_ptr<::sweep_device, decltype(&::sweep_device_destruct)> device;
};
// Implementation
namespace detail {
struct error_to_exception {
operator ::sweep_error_s*() { return &error; }
~error_to_exception() noexcept(false) {
if (error) {
device_error e{::sweep_error_message(error)};
::sweep_error_destruct(error);
throw e;
}
}
::sweep_error_s error = nullptr;
};
}
sweep::sweep(const char* port)
: device{::sweep_device_construct_simple(port, detail::error_to_exception{}), &::sweep_device_destruct} {}
sweep::sweep(const char* port, std::int32_t bitrate)
: device{::sweep_device_construct(port, bitrate, detail::error_to_exception{}), &::sweep_device_destruct} {}
void sweep::start_scanning() { ::sweep_device_start_scanning(device.get(), detail::error_to_exception{}); }
void sweep::stop_scanning() { ::sweep_device_stop_scanning(device.get(), detail::error_to_exception{}); }
bool sweep::get_motor_ready() { return ::sweep_device_get_motor_ready(device.get(), detail::error_to_exception{}); }
std::int32_t sweep::get_motor_speed() { return ::sweep_device_get_motor_speed(device.get(), detail::error_to_exception{}); }
void sweep::set_motor_speed(std::int32_t speed) {
::sweep_device_set_motor_speed(device.get(), speed, detail::error_to_exception{});
}
std::int32_t sweep::get_sample_rate() { return ::sweep_device_get_sample_rate(device.get(), detail::error_to_exception{}); }
void sweep::set_sample_rate(std::int32_t rate) {
::sweep_device_set_sample_rate(device.get(), rate, detail::error_to_exception{});
}
scan sweep::get_scan() {
using scan_owner = std::unique_ptr<::sweep_scan, decltype(&::sweep_scan_destruct)>;
scan_owner releasing_scan{::sweep_device_get_scan(device.get(), detail::error_to_exception{}), &::sweep_scan_destruct};
auto num_samples = ::sweep_scan_get_number_of_samples(releasing_scan.get());
scan result;
result.samples.reserve(num_samples);
for (std::int32_t n = 0; n < num_samples; ++n) {
auto angle = ::sweep_scan_get_angle(releasing_scan.get(), n);
auto distance = ::sweep_scan_get_distance(releasing_scan.get(), n);
auto signal = ::sweep_scan_get_signal_strength(releasing_scan.get(), n);
result.samples.push_back(sample{angle, distance, signal});
}
return result;
}
void sweep::reset() { ::sweep_device_reset(device.get(), detail::error_to_exception{}); }
} // ns
#endif
A simplified version of your problem:
buggy.hpp
int function() { return 0; }
main.cpp
#include "buggy.hpp"
int main() { return 0; }
other.cpp
#include "buggy.hpp"
The problem is that buggy.hpp is defining function, not just declaring. Once the header inclusion is expanded, that means function is declared in both main.cpp and other.cpp - and that is not allowed.
The fix is to declare function as inline which allows the function to be declared in multiple translation units.
inline int function() { return 0; }
In fact, allowing multiple definitions is the only meaning of inline to the C++ standard. Compilers may treat it as a hint that the function body may be expanded inline. Good ones won't; they are better at making that sort of decision that programmers).

Proper use of namespaces for function definitions in cpp file

So for some reason I have experienced the behavior that adding a namespace to my .h and .cpp files for a set of functions breaks my linker. I'm using Visual Studio 2012. Here's my scenario (simplified)
functions.h
int functionA();
int functionB();
functions.cpp
#include "functions.h"
int functionA() { return 0; }//we could pretend there's actual code here
int functionB() { return 0; }//we could pretend there's actual code here
and the actual useage of it is in some cpp file thusly:
pointers.h
#include "functions.h"
class GetPointers
{
public:
typedef int (*FunctionPointer)(void);
static FunctionPointer funcPointerA() { return &functionA; }
static FunctionPointer funcPointerB() { return &functionB; }
};
Well that's all fine and dandy. I can call the static method of GetPointers and get a function pointer which works. Everythings been tested and everything is happy. Now I thought I would simply add some namespaces to make sure I don't have any problems again in the future. So I simply modify the three code files to use namespaces. What happens is a link error which refers to the function funcPointerA() and funcPointerB() of the GetPointers class, with the full namespace name to functionA and functionB.
functions.h
namespace fun {
int functionA();
int functionB();
}
functions.cpp
#include "functions.h"
using namespace fun;
int functionA() { return 0; }//we could pretend there's actual code here
int functionB() { return 0; }//we could pretend there's actual code here
and the actual useage of it is in some cpp file thusly:
pointers.h
#include "functions.h"
namespace fun {
class GetPointers
{
public:
typedef int (*FunctionPointer)(void);
static FunctionPointer funcPointerA() { return &functionA; }
static FunctionPointer funcPointerB() { return &functionB; }
};
}
I don't get a build error, only a link error about fun::functionA and fun::functionB. Is there something implicitly wrong with using function pointers from namespaces?
The problem is that your definitions:
int functionA() { return 0; }
int functionB() { return 0; }
are in the global namespace; so they declare new functions there rather than define the functions declared in namespace fun.
The best fix is to qualify the names in the definitions:
int fun::functionA() { return 0; }
int fun::functionB() { return 0; }
This is preferable to putting the definitions inside the namespace, since it gives a compile-time check that the functions match their declarations.

include graphic interface on program

Like title say, I think i have a problem on one of my include.
I'm studient, my teacher gave me a solution to create a visual interface (coded by himself) named EZ-Draw.
But somewhere in my code there is a problem 'cause my compiler tell me many errors of this style:
|364|undefined reference to `CreateCompatibleDC#4'|
|559|undefined reference to `SelectObject#8'|
...
my code:
interpreteur.hpp
#ifndef INTERPRETEUR_HPP_INCLUDED
#define INTERPRETEUR_HPP_INCLUDED
#include <sstream>
#include <map>
#include <math.h>
#include "Pile_Template.hpp"
#include "ez-draw++.h"
#define PI 3.14159265
class Interpreteur {
private:
void (Interpreteur::*ptr)();
EZWindow myWindow;
Pile<double> pile;
Pile<string> pilestr;
bool run;
public:
map<string,void(Interpreteur::*)()> myMap;
Interpreteur();
~Interpreteur();
inline bool getRun() {return run;};
inline void setEmpilerPile(double nombre) {pile.empiler(nombre);};
template <typename T>
string itos(T nombre) // convertit un double en string
{
ostringstream ss;
ss<<nombre;
return ss.str();
}
void addition();
void moins();
void multiplie();
void divise();
void quit();
void push();
void pushstr();
void pop();
void popstr();
void copy();
void copystr();
void print();
void printstr();
void display();
void displaystr();
void count();
void countstr();
void swap();
void swapstr();
void sinus();
void cosinus();
void tangente();
void racine();
void trunc();
void line();
void color();
void drawstr();
void triangle();
void rectangle();
void circle();
};
#endif // SOUS_PROGRAMMES_HPP_INCLUDED
interpreteur.cpp
#include "interpreteur.hpp"
#include <sstream>
#include <map>
#include <math.h>
#include <string>
using namespace std;
void Interpreteur::addition()
{
pile.empiler(pile.depiler()+pile.depiler());
}
void Interpreteur::moins()
{
double nombre=pile.depiler();
nombre=pile.depiler()-nombre;
pile.empiler(nombre);
}
void Interpreteur::multiplie()
{
pile.empiler(pile.depiler()*pile.depiler());
}
void Interpreteur::divise()
{
double nombre=pile.depiler();
nombre=pile.depiler()/nombre;
pile.empiler(nombre);
}
void Interpreteur::quit()
{
run=false;
}
void Interpreteur::push()
{
double i;
cin>>i;
pile.empiler(i);
}
void Interpreteur::pushstr()
{
string chaine;
char merde;
cin>>merde;
if(merde=='"')
{
getline(cin,chaine,'"');
pilestr.empiler(chaine);
}
else
{
cin.putback(merde);
cerr<<"mauvaise chaine de caractères"<<endl;
}
}
void Interpreteur::pop()
{
pile.depiler();
}
void Interpreteur::popstr()
{
pilestr.depiler();
}
void Interpreteur::copy()
{
int i=pile.depiler();
pile.empiler(pile[pile.getSommet()-i]);
}
void Interpreteur::copystr()
{
int i=pile.depiler();
pilestr.empiler(pilestr[pile.getSommet()-i]);
}
void Interpreteur::print()
{
cout<<pile.depiler()<<endl;
}
void Interpreteur::printstr()
{
cout<<pilestr.depiler()<<endl;
}
void Interpreteur::display()
{
pile.afficher(cout);
}
void Interpreteur::displaystr()
{
pilestr.afficher(cout);
}
void Interpreteur::count()
{
pile.empiler(pile.getSommet());
}
void Interpreteur::countstr()
{
pilestr.empiler(itos(pilestr.getSommet()));
}
void Interpreteur::swap()
{
double first=pile.depiler();
double second=pile.depiler();
pile.empiler(first);
pile.empiler(second);
}
void Interpreteur::swapstr()
{
string first=pilestr.depiler();
string second=pilestr.depiler();
pilestr.empiler(first);
pilestr.empiler(second);
}
void Interpreteur::sinus()
{
pile.empiler(sin(pile.depiler()*PI/180));
}
void Interpreteur::cosinus()
{
pile.empiler(cos(pile.depiler()*PI/180));
}
void Interpreteur::tangente()
{
pile.empiler(tan(pile.depiler()*PI/180));
}
void Interpreteur::racine()
{
pile.empiler(sqrt(pile.depiler()));
}
void Interpreteur::trunc()
{
int x=pile.depiler();
pile.empiler(x);
}
void Interpreteur::line()
{
int y2=pile.depiler();
int x2=pile.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawLine(x1,y1,x2,y2);
}
void Interpreteur::color()
{
int couleur=pile.depiler();
switch(couleur)
{
case 1:{myWindow.setColor(ez_black);break;}
case 2:{myWindow.setColor(ez_red);break;}
case 3:{myWindow.setColor(ez_green);break;}
case 4:{myWindow.setColor(ez_blue);break;}
case 5:{myWindow.setColor(ez_cyan);break;}
case 6:{myWindow.setColor(ez_magenta);break;}
case 7:{myWindow.setColor(ez_yellow);break;}
//pourquoi que on a pas fait le gris ? ez_grey
default:{pile.empiler(couleur); cerr<<"couleur inconnue"<<endl; break;}
}
// COULEUR : ez_black, ez_white, ez_grey, ez_red, ez_green, ez_blue,ez_yellow, ez_cyan, ez_magenta
}
void Interpreteur::drawstr()
{
string str=pilestr.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawText(EZ_MC,x1,y1,str);
}
void Interpreteur::triangle()
{
int y3=pile.depiler();
int x3=pile.depiler();
int y2=pile.depiler();
int x2=pile.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawTriangle(x1,y1,x2,y2,x3,y3);
}
void Interpreteur::rectangle()
{
int y2=pile.depiler();
int x2=pile.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawRectangle(x1,y1,x2,y2);
}
void Interpreteur::circle()
{
int y2=pile.depiler();
int x2=pile.depiler();
int y1=pile.depiler();
int x1=pile.depiler();
myWindow.drawCircle(x1,y1,x2,y2);
}
Interpreteur::Interpreteur()
{
run=true;
myMap["+"]=&Interpreteur::addition;
myMap["-"]=&Interpreteur::moins;
myMap["*"]=&Interpreteur::multiplie;
myMap["/"]=&Interpreteur::divise;
myMap["exit"]=&Interpreteur::quit;
myMap["push"]=&Interpreteur::push;
myMap["pushstr"]=&Interpreteur::pushstr;
myMap["pop"]=&Interpreteur::pop;
myMap["popstr"]=&Interpreteur::popstr;
myMap["copy"]=&Interpreteur::copy;
myMap["copystr"]=&Interpreteur::copystr;
myMap["print"]=&Interpreteur::print;
myMap["printstr"]=&Interpreteur::printstr;
myMap["display"]=&Interpreteur::display;
myMap["displaystr"]=&Interpreteur::displaystr;
myMap["count"]=&Interpreteur::count;
myMap["countstr"]=&Interpreteur::countstr;
myMap["swap"]=&Interpreteur::swap;
myMap["swapstr"]=&Interpreteur::swapstr;
myMap["sin"]=&Interpreteur::sinus;
myMap["cos"]=&Interpreteur::cosinus;
myMap["tan"]=&Interpreteur::tangente;
myMap["sqrt"]=&Interpreteur::racine;
myMap["trunc"]=&Interpreteur::trunc;
myMap["line"]=&Interpreteur::line;
myMap["color"]=&Interpreteur::color;
myMap["drawstr"]=&Interpreteur::drawstr;
myMap["triangle"]=&Interpreteur::triangle;
myMap["rectangle"]=&Interpreteur::rectangle;
myMap["circle"]=&Interpreteur::circle;
}
Interpreteur::~Interpreteur()
{
map<string, void (Interpreteur::*)()>::iterator it;
myMap.erase(it);
}
and here the "EZ-Draw documentation" gived by my teacher to understand ez-draw and ez-draw++
I don't understand what the compiler is trying to tell me
You are using C functions from C++, for this to work you need to explicitly tell the compiler those are C functions. The C header file you are using from C++ should contain those lines to be usable in C++:
#ifdef __cplusplus
extern "C" {
#endif
//The header file which declares the functions which are not linked correctly here
#ifdef __cplusplus
}
#endif
A bit of explanation:
The function "name mangling" is different for C and C++, since in C++ you can overload a function the name of the function alone does not identify a function uniquely, so the compiler adds some symbols to the names of functions in background when you compile to make those names unique (that's the DC#4 in CreateCompatibleDC#4).
Since your linker expects C++ functions, it searches for CreateCompatibleDC#4, but some of your files get compiled in C, and export a function named CreateCompatible, that's why you get a "undefined reference": the linker is telling you it can not find the definition of some functions.