I want to write a function, which could receive a ostream, a iterable object v, and a function op. The function is supposed to call op on every element in v, and then send the result to ostream. So I write the following code: template<typename T, typename IN = decltype(*std::declval<T>().begin()), typename FT = function<ostream&(ostream&, const ..
Category : c++11
I it possible to define C++ constants of complex types such as #define std::map<std::size_t, double> SHELL_DISTANCE_MAP = { {0, SHELL_K_DISTANCE}, {1, SHELL_K_DISTANCE}, {2, SHELL_L_DISTANCE}, … It’s failing for me so I assume not. Yet, can you please confirm? Or is there an alternative? I need the above map as a global constant and do not ..
In C++11 I have a problematic line, when it’s commented my programs runs perfect. when I uncomment it it causes tons of errors. My line is: jobs.insert(std::lower_bound(jobs.begin(), jobs.end(), job), job); In the following function: void JobsList::addJob(Command *cmd, bool isStopped) { JobEntry tmp{}; tmp.pid = 0; //getLastJob(&tmp.jid); ++tmp.jid; tmp.stopped = isStopped; tmp.cmd = cmd->cmd; time(&tmp.in_time); for ..
I want to implement a function which iterates over a list and returns a pointer in case of a match. I wrote: std::list<JobEntry> jobs; JobsList::JobEntry *JobsList::getJobById(int jobId) { for (auto const& job : jobs) { if (job.pid==jobId) { return std::addressof(*job); } } return nullptr; } But this doesn’t work, how can I do it? Source: ..
Example 1 Consider the following basic class: class X { int x; public: void set_x (int new_x) {x = new_x;} void get_x () const {return x;} } Is set_x thread safe? Off course, set_x is not thread safe as calling it from two threads simultaneously results in a data race. Is get_x thread safe? Two ..
Suppose here new will raise exception. Will the NULL condition below new of object will ever be checked as on exception it will return , So is there point to have this NULL check after every new? #include <iostream> using namespace std; class Obj { int x; public: Obj() : x(0) { } ~Obj() = ..
/Write a C++ program in which, read a six digit integer as input from user and output each of its digit in words by using loop./ #include <iostream> using namespace std; int main() { int number, last_number, count = 0; cout << "Please enter a six digit number:" << endl; cin >> number; while (count ..
Popular opinion is that C++ array is safer with almost equal efficiency. Except for checking whether the index is out of range and not allowing implicit type cast to pointer, are there any other features? Besides, why implicit type cast of traditional C array is regarded as a bad way? I’m not very good at ..
I have some simple rendering program with a Mainloop that runs at about 8000 fps on one thread (it does nothing except draw a background) and I wanted to see if another thread rendering would upset the current context without changing it (it didn’t to my surprise). I achieved this with this simple code here, ..
I have a very simple C++ code statement auto a = 12;. When I am compiling it with g++ in Linux using -std=c++98 option I am getting an error as expected error: ‘a’ does not name a type But when I am compiling the same code with the same option in MacOS I am getting ..
Recent Comments