Category : list-initialization

In the next program the second non-type template argument of struct A is initialized with {} in the alias template B<T>: template<class T, T> struct A{}; template<class T> using B = A<T, {}>; B<int> b; GCC is the only compiler accepting this. Both Clang and MSVC reject the program with similar errors. Clang: error: expected ..

Read more

You might feel code is long and I write lot of things but believe me it is quite simple. I referred these two answers on stack-overflow for understanding this. post1 & post2 Code #include <iostream> class A { public: int k1=3; float f1=34.20; char ch1=’a’; std::string str1="bonaparte"; void display() { std::cout<<k1<<" "<<f1<<" "<<ch1<<" "<<str1<<" xxn"; ..

Read more

From C++11 and above, I can construct a class with list initialization std::vector<int> foo{1,2,3} or copy list-initialization with std::vector<int> foo = {1,2,3}. Assuming I have a class as follows class Event{ public: int time, Bar* ptr Event() = default; Event(int t, Bar* p):time(t),ptr(p){}; } What kind of constructor should I define so that I can ..

Read more

I am trying to implement a bottom up approach function to the rod cutting problem and I need to use this particular pseudo-code from the CLRS textbook. In it there two functions and one calls the other in this fashion (r,s) = EXTENDED-BOTTOM-UP-CUT-ROD(p,n) Where r and s are two different arrays. The function also returns ..

Read more

Here’s the setup to illustrate the problem. template <typename T> struct opt { const char* name; T x; template <typename X> opt(const char* name, X&& x) : name(name), x(std::forward<X>(x)) { } }; template <typename T> opt(const char*, T&&) -> opt< some_type_transformation<T> >; template <typename… T> void function(opt<T>&&… opts) { // do something with the passed ..

Read more

In C++ Primer 2.2.1 Variable Definitions chapter They said initialize with a curly brace is called list initialize and they said When used with variables of built-in type, this form of initialization has one important property: The compiler will not let us list initialize variables of built-in type if the initializer might lead to the ..

Read more