c++98

“Overload” function template based on function object operator() signature in C++98

若如初见. 提交于 2019-11-28 09:34:40
问题 I want to make a template function that takes a function and a vector and uses the function to map that vector to another vector that will be returned by the function template. If the function taken as an argument is a free function, it may have one of two signatures. // T is the parameter of the function template T sig1(const T x); T sig2(const T x, const std::vector<T>& v); It may also be a function object in which operator() would behave like the free functions. Use of the function

Using boost::assign::list_of

拥有回忆 提交于 2019-11-27 23:43:23
问题 This compiles: std::vector<int> value = boost::assign::list_of(1)(2); But not this: Constructor(std::vector<int> value) { } Constructor (boost::assign::list_of(1)(2)); Is there a one-liner solution for initializing the vector passed to the constructor? Better still, if the constructor copies to a class variable by taking a reference instead: Constructor(std::vector<int>& value) { _value = value; } UPDATE If I try the following: enum Foo { FOO_ONE, FOO_TWO }; class Constructor { public:

“Constant expressions” prior to C++11

浪尽此生 提交于 2019-11-27 23:34:11
问题 The constexpr keyword was introduced in C++11, as (I think) was the corresponding idea of "constant expressions." However, this concept was implicitly present in C++98/c++03, since array declarations require a constant expression: // valid: int a[sizeof(int)]; int b[3+7]; int c[13/4]; const int n = 3; int d[n]; // invalid: int m = 4; int e[m]; There are other "constant expressions", i.e., expressions that can be (and/or must be) evaluated at compile-time; one example is template arguments.

Time complexity of removing items in vectors and deque

别说谁变了你拦得住时间么 提交于 2019-11-27 17:32:00
问题 I have read that time complexity of adding items to end of a std::vector is amortized constant and inserting items at the top and bottom of a std::deque is constant.Since both these containers have a random access iterator thus accessing elements at any index is constant. Please let me know if I have any of these facts wrong.My question is if accessing an element in a std::vector or std::deque is constant then why is the time complexity of removing an element via erase O(n). One of the

Default, value and zero initialization mess

只愿长相守 提交于 2019-11-27 16:59:26
I am very confused about value- & default- & zero-initialization. and especially when they kick in for the different standards C++03 and C++11 (and C++14 ). I am quoting and trying to extend a really good answer Value-/Default-/Zero- Init C++98 and C++03 here to make it more general as it would help a lot of users if somebody could help fill out the needed gaps to have a good overview about what happens when? The full insight by examples in a nutshell: Sometimes the memory returned by the new operator will be initialized, and sometimes it won't depending on whether the type you're newing up is

How to do static_assert with macros?

醉酒当歌 提交于 2019-11-27 15:57:14
I have tried to use this suggestion to do a static assert, but I do not get a compilation error if I use it within a method of a template. The example follows : #include <iostream> #define STATIC_ASSERT(expr, msg) \ { \ char STATIC_ASSERTION__##msg[(expr)?1:-1]; \ (void)STATIC_ASSERTION__##msg[0]; \ } template <typename T > class A { public: int foo(const int k ) { // does not work STATIC_ASSERT( k > 9, error_msg ); return k+5; } }; int bar(const int k ) { // works fine //STATIC_ASSERT( k > 9, error_msg ); return k+5; } int main() { A<int> a; const int v = 2; std::cout<<a.foo(v)<<std::endl;

C++11 Exception's destructor allows to throw now?

江枫思渺然 提交于 2019-11-27 07:36:24
问题 any idea why virtual ~exception() throw() is in C++98, but virtual ~exception() is in C++11? What's the design decision that allows C++11 to throw in the destructor of the class exception ? From here: c++98: class exception { public: exception () throw(); exception (const exception&) throw(); exception& operator= (const exception&) throw(); virtual ~exception() throw(); virtual const char* what() const throw(); } c++11: class exception { public: exception () noexcept; exception (const

Vector of const objects giving compile error

自作多情 提交于 2019-11-27 02:03:32
I have declared the following in my code vector <const A> mylist; I get the following compile error - new_allocator.h:75: error: `const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const \[with _Tp = const A]' and `_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const A]' cannot be overloaded But if declare - vector <A> mylist; my code compiles. Is const not allowed in this context? I am copying my code here for everyone'e reference - #include <iostream> #include <vector> using namespace std; class A { public: A () {cout << "default constructor\n";} A (int i): m

Default, value and zero initialization mess

人走茶凉 提交于 2019-11-26 18:48:57
问题 I am very confused about value- & default- & zero-initialization. and especially when they kick in for the different standards C++03 and C++11 (and C++14 ). I am quoting and trying to extend a really good answer Value-/Default-/Zero- Init C++98 and C++03 here to make it more general as it would help a lot of users if somebody could help fill out the needed gaps to have a good overview about what happens when? The full insight by examples in a nutshell: Sometimes the memory returned by the new

How to do static_assert with macros?

こ雲淡風輕ζ 提交于 2019-11-26 14:50:14
问题 I have tried to use this suggestion to do a static assert, but I do not get a compilation error if I use it within a method of a template. The example follows : #include <iostream> #define STATIC_ASSERT(expr, msg) \ { \ char STATIC_ASSERTION__##msg[(expr)?1:-1]; \ (void)STATIC_ASSERTION__##msg[0]; \ } template <typename T > class A { public: int foo(const int k ) { // does not work STATIC_ASSERT( k > 9, error_msg ); return k+5; } }; int bar(const int k ) { // works fine //STATIC_ASSERT( k > 9