Newest 'c++11' Questions

Q&A for professional and enthusiast programmers

trying to create an adaptor file for qt application via xml

below is the xml code for creating adaptor Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> <method name="deleteAllItems"> &...

Validation input for Do-While Loop (Y / N) not working properly. C++

The do while loop did work when I used cin >> yesN; However it doesn't work with scanf_s . Please if you have any advice I would appreciate it. The main part is the Do-While and its input validation,...

How to write this mathematical formula in C++? [on hold]

I have a simple formula and I want to use it in C++ application. Not sure how to rewrite in C++.

C++ : elegantly iterate a set of numbers

Could anyone suggest how in C++11/14 to elegantly iterate a constant set (in english meaning, not C++ meaning) of numbers, preferably without leaving temporary objects like here: set<int> ...

casting std::function to a different std::function and invoking it?

I have a question about casting an std::function of one type to another with the same number of arguments or more and then invoking it since it works on all the compilers but i am not sure if it is a ...

Allowing both pre-computed and computed-on-the-fly results

Consider: template <typename T> struct C { std::vector<int> f(const T &t) const { return t.f(); } }; T::f must compute the required vector. However, some T's pre-...

Difference between std::forward implementation

Recently I've been trying to understand move semantics and came up with a question. The question has already been discussed here. I implemented the first variant and checked whether it returns l-...

Why this inline method with deduced return type not defined yet?

#include <string> #include <type_traits> class C { static auto func() { return std::string("hello"); } static_assert(std::is_same<decltype(func()), std::string>::value, ""); ...

currency converter program in c++ using classes [on hold]

Actually I have a problem related to a program of currency converter. I am a beginner and I don't have much knowledge of voiding in c++. I have made the currency converter coding without the concept ...

Null terminator \1?

I am doing some coding for a beginner C++ class I am taking. In the class, we have to take code submitted by another student and fix a bug they created. The code is as follows: #include <iostream&...

Detect dangling references to temporary

Clang 3.9 extremely reuses memory used by temporaries. This code is UB (simplified code): template <class T> class my_optional { public: bool has{ false }; T value; const T& ...

Why is std::forward necessary with forwarding references

In a function template like this template <typename T> void foo(T&& x) { bar(std::forward<T>(x)); } Isn't x an rvalue reference inside foo, if foo is called with an rvalue ...

Forwarding the Return Value of a Generic Callable Object

The code below forwards a parameter pack to some user-defined callable object (in this case, a function pointer). Is there a way in C++11 to return the callable's return value? More specifically, I ...

pass by reference of thread object to another API [on hold]

I have created a new thread using std::thread threadHandle; Now i want to pass this to another function, can anyone tell me how to pass via reference.

casting void* to std::function

I have an issue. I'm trying to convert a void* to std::function. This is just a simple example, any suggestions will be appreciated #.h file class Example { public: Example(); int foo(void* hi);...

Dynamic memory allocation in Eigen

I am using Eigen C++ library and I would like to see if my code uses dynamic memory allocation. According to documentation (https://eigen.tuxfamily.org/dox/TopicPreprocessorDirectives.html), #defining ...

Is there a decltype equivalent in Swift?

In C++11 and latter, it is possible to use a compile time function like syntax named decltype() to reuse declaration type of a variable to declare another one without rewriting the type signature. Is ...

shared_ptr with multiple interfaces

Consider the following classes: class MyInterface1 { ... }; class MyInterface2 { ... }; class MyClass : public MyInterface1, public MyInterface2 { ... }; If I have the following code: ...

C++ unordered set issue with struct hash [duplicate]

I'm relatively new to C++ and I feel like I'm in over my head. I'm trying to create a graph structure that can take any kind of data by using templates. Here is c_graph.h #pragma once #include <...

when i open a file, nothing will be displayed - C++

Premise: I'm using CLion. As i said in title, when i try to open a file (txt) nothing will be displayed. i can't explain it, i don't think i made an error, it's pretty easy this code: #include <...

Does the pointer returned by std::poiter_traits<Ptr>::pointer_to compare equal to the pointer returned by the allocator?

I would like to know whether the pointer returned by the static member function std::pointer_traits<Ptr>::pointer_to(element_type&...

C++: Unpack a tuple with references and values without copying/moving too much

Let's say I have a tuple t whose type is std::tuple<T1,T2,T3> where each T may be either Obj, Obj&, Obj&&,const Obj&. I want to write a function which unpacks the values of the ...

How to take inputs from array and input it into an equation in C++?

My goal is to try and create a program that takes in grades in percents and multiply it with their weight value (either in decimal form or percent form). The equation is basically: Overall grade = (...

Call an “initialize” function in an object when one of its member functions are called

I am trying to implement lazy initializing in C++ and I am searching for a nice way to call the Initialize() member function when some other method like object->GetName() gets called. Right now I ...

Explicit instantiation for concept checking

We have: template<typename T> struct A { void foo(int a) { T::foo(a); } }; template<typename T> struct B { template struct A<T>; // concept check }; So, I define a ...

Can I use a locally declared enum class as template non-type parameter? (gcc gives obscure error)

The following code fails to compile in gcc 4.8.1 thru 6.3: #include <cassert> template<typename T, T X> struct Mode { using type = Mode; using value_type = T; static ...

Repeated values from std::uniform_int_distribution

I don't understand what's going on here. #include <iostream> #include <random> #include <chrono> using namespace std; unsigned number_in_range(unsigned, unsigned, ...

SublimeLinter with C++11

I've started using Sublime Text 3 as my code editor for C++ and I installed some additional packages, two of which being SublimeLinter and SublimeLinter-contrib-clang. Everything seems to be working ...

Use of template<typename>

Say I have code like: #include <iostream> template<typename> void printnum(int i) { std::cout<<"in int method:"<<std::endl; std::cout<< i<<std::endl; } ...

CMake fails identifying C++ compiler version in new builds

Something quite weird is happening to me: CMake fails to choose the correct version of g++ only if I compile a project for the first time (or I delete 'build' folder). So a 'fresh' compilation brings ...

How is this code giving SEGMENTATION FAULT? [on hold]

#include <iostream> using namespace std; class Node{ public: int i; Node *left,*right; }; int main() { Node *a = new Node; Node l; Node r; a->i = 5; ...

Differentiating Specialized Structs based on signed-ness

I need to be able to differentiate between data types which have the same sizes but different signed-ness properties. An example follows, template <size_t N> struct alias; template<> ...

Refactoring tag dispatching structure

I have following code: #include <iostream> #include <cassert> #include <utility> struct real_type { struct real_category{}; typedef real_category num_category; }; ...

When to make copy and assign constructors delete in c++11?

I often see the code that deletes 1)copy ctor and 2)assign ctor. What is the advantage to do so? I append sample code. class A{ ... private: A(const A& copy) = delete; A ...

use lambda in function template, can't deduce type, makeSet() use case

What I want to achieve is a makeSet() function accepting three arguments, a pair of iterator, and a function that transforms the value. One use case could be creating a set from a sequence of values ...

get type of element in range based loop **directly** e.g. for “using”

How to get type of return value of ranged-base loop elegantly? I currently use this :- (it works OK) using Encapsulator= std::vector<int>; //<-- any datastructure using ReturnType = ...

Expanding variadic non-type template arguments inside lambda - gcc vs clang [duplicate]

Consider the following code: template <int... Is> void foo() { using arr = int(*[])(); (void) arr{[]{ return Is; }...}; } int main() { foo<1,2,3>(); } clang++ 3.3 (up to 5.0) ...

How to pass a lambda-function-variable with arguments to std::thread? [duplicate]

More answers: C++ Thread taking reference argument failed compile I'm creating a callable variable to which I'm assigning a lambda-function with arguments: auto func = [](int &d) -> void { d++...

What is the point of using delete on a non-member function?

Excerpt from the Standard 20.12 [function.objects] : template <class T> reference_wrapper<T> ref(T&) noexcept; template <class T> reference_wrapper<const T> cref(const T&...

Static Bool Array Not initializing as set

Why are my array of static bools not initialized properly? Only the first one is initialized - I suspect this is because the array is static. The following MWE was compiled with GCC and is based on a ...

How does the 'delete' operator actually work behind the scenes in C++ in dynamic memory allocation (heap)?

I am not getting how the "delete" operator is actually implemented behind the scenes in C++. For example: class Node{ int i; Node *left,*right; }; int main() { Node* a = new Node; // ...

Define with scope inside

I would like to jump over some scope to access directly to a static variable without write all of my scope. nts::CLI::Mode::AMode::Out where Out is pointer to static variable to the one I would like ...

Rvalue reference usage in sample is_copy_asignable implementation

I am watching Walter Brown's talk on CppCon 2014 'Modern Template Metaprogramming: A Compendium'. In part two, he demonstrates the usage of decltype and declval with a sample implementation of ...

hash function for the “find the line that contains the maximum number of points in P”

Here is the passage from the Elements of Programming Interviews book: Let P be a set of n points in the plane. Each point has integer coordinates. Design an efficient algorithm for computing a ...

error: use of parameter outside function body before ‘]’ token [on hold]

Why am i getting this error in C++0x ? I do understand i would get this if row wasn't const, but since it is, i do not understand the error. error: use of parameter outside function body before ‘]’ ...

Custom std::set comparator - No match for call to […]

I have applied the accepted answer to this question in a templated class, like this: template <typename T, typename S> struct sizeCompare { bool operator() (const std::set<Node<T, S&...

How to cache (store) shared_ptr with class using template?

I'm rewriting a class using templates. Before the code looked like below. The change is to add a template to the bar class. However, after changing, the cache map is not working, since I could not ...

Override only operator*() of iterator of a custom datastructure

I have an array class MyArray with MyArray. For simplicity, this is the whole code. It works OK. Current code #include <iostream> using namespace std; template<class T> class MyArray; ...

C++11 constexpr+noexcept doesn't tell if a function is compile-time constant function

I see this example from cppreference website, but the running result was not anything expected.(http://en.cppreference.com/w/cpp/language/constexpr) The page says:" Because the noexcept operator ...

Mapping a std::tuple to another std::tuple

Let's assume I have a std::tuple<Types...>, I want to generate a std::tuple<std::priority_queue<Types...>> #include <queue> #include <tuple> template<typename ...
Translating... 0%