operator-overloading

Generically overloading operator new while considering alignment requirements

徘徊边缘 提交于 2020-01-14 03:23:07
问题 Situation I am writing a memory manager for dynamic memory (de)allocations. For a class A to use it when operator new (or delete ) is called, it is sufficient for class A to inherit from a class CustomAllocate , which itself overloads new and delete in a way that uses the memory manager. Problem However, apparently I completely missed out on alignment requirements. Unfortunately, CustomAllocate::new has no information about how a class A inheriting from it should be aligned as the only

error: address of overloaded function with no contextual type information

不想你离开。 提交于 2020-01-13 19:47:27
问题 Code: class que { public: que operator++(int) {} // 1 que &operator++() {} que &operator+=(int n) { que& (que::*go)(); go = 0; if(n > 0) go = &que::operator++ ; // 2 //go = (n > 0) ? (&que::operator++) : 0 ; // 3 } }; int main() { que iter; iter += 3; return 0; } I want to replace line 2 by line 3("if" statement for "?:"). If I uncomment 3, compiler gives me an error. If I delete line 1, then line 3 works. Question is: what does compiler want from me ? Error: error: address of overloaded

Is there any drawbacks in providing operator+ or operator- to bidirectional iterators?

夙愿已清 提交于 2020-01-13 12:08:28
问题 The bidirectional iterators have no luxuries like random access iterators, and hence need to depend upon the std::next and std::prev, when someone needs to do operations like std::set<int> s{ 1, 2, 3, 4, 5 }; //std::set<int> s2(s.cbegin(), s.cbegin() + 2); // won't work as there is no operator+ for std::set::const_iterator std::set<int> s2(s.cbegin(), std::next(s.cbegin(), 2)); //std::set<int> s3(s.cbegin(), s.cend() - 2); // won't work as there is no operator- for std::set::const_iterator

Is there any drawbacks in providing operator+ or operator- to bidirectional iterators?

你说的曾经没有我的故事 提交于 2020-01-13 12:08:15
问题 The bidirectional iterators have no luxuries like random access iterators, and hence need to depend upon the std::next and std::prev, when someone needs to do operations like std::set<int> s{ 1, 2, 3, 4, 5 }; //std::set<int> s2(s.cbegin(), s.cbegin() + 2); // won't work as there is no operator+ for std::set::const_iterator std::set<int> s2(s.cbegin(), std::next(s.cbegin(), 2)); //std::set<int> s3(s.cbegin(), s.cend() - 2); // won't work as there is no operator- for std::set::const_iterator

Is it OK to inherit from the C++11 smart pointers and override the relative operators?

亡梦爱人 提交于 2020-01-12 12:53:05
问题 According to cppreference.com, std::shared_ptr provides a full set of relative operators (==, !=, <, ...), but the semantics of comparison aren't specified. I assume they compare the underlying raw pointers to the referenced objects, and that std::weak_ptr and std::unique_ptr do the same. For some purposes, I would prefer to have relative operators that order the smart pointers based on comparing the referenced objects (rather than the pointers to them). This is already something I do a lot,

How to print the values of two dimensional pair vector?

泪湿孤枕 提交于 2020-01-11 13:33:33
问题 I have a text file with 4 rows and 3 columns (0.165334,0) (0.166524,-0.0136064) (-0.144899,0.0207161) (0.205171,0) (0.205084,-0.0139042) (-0.205263,0.0262445) (0.216684,0) (0.215388,-0.0131107) (-0.193696,0.0251303) (0.220137,0) (0.218849,-0.0135667) (-0.194153,0.025175) I wrote following code to print the values of FFTfile . The script does not throw any errorm but it does not print the values. Any idea whats wrong? #include <iostream> #include <stdio.h> #include <fstream> #include <stdlib.h

How to print the values of two dimensional pair vector?

拜拜、爱过 提交于 2020-01-11 13:32:09
问题 I have a text file with 4 rows and 3 columns (0.165334,0) (0.166524,-0.0136064) (-0.144899,0.0207161) (0.205171,0) (0.205084,-0.0139042) (-0.205263,0.0262445) (0.216684,0) (0.215388,-0.0131107) (-0.193696,0.0251303) (0.220137,0) (0.218849,-0.0135667) (-0.194153,0.025175) I wrote following code to print the values of FFTfile . The script does not throw any errorm but it does not print the values. Any idea whats wrong? #include <iostream> #include <stdio.h> #include <fstream> #include <stdlib.h

c++ overloading stream operator, parameters by reference and anonymous instances

萝らか妹 提交于 2020-01-11 09:56:09
问题 If I have a POD with an overloaded stream operator: struct Value{ ... friend ostream& operator<< (ostream &out, Value &val); ... }; I can't use the stream operator with anonymous instances. for example I can't do: cout<<Value(); This gives me: error: ambiguous overload for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘Value’) On the other hand, I can pass the POD by value, but I would like to avoid the copy. Is there a way to both? Value v1; cout<<v1<<" "<

Overload operator<< for nested class template

寵の児 提交于 2020-01-11 08:51:09
问题 I have the following setup: template< class T > struct Foo { struct Bar { Bar ( const T &t ) : otherT_( t ) {} T otherT_; }; Foo ( const T &t ) : myT_( t ) {} T myT_; }; Now, I want to make instances of Foo< T >::Bar streamable to std::cout and friends. I tried this: template< class T > std::ostream& operator<< ( std::ostream &os, const typename Foo< T >::Bar &bar ) { os << "<bar: " << bar.otherT_ << ">"; return os; } But the following code does not compile: Foo< int > foo( 5 ); Foo< int >:

Ambiguous definition of operator() with multiple inheritance

旧时模样 提交于 2020-01-11 08:23:06
问题 I compile this code with GCC (4.2.1 Apple build 5664) #include <cstddef> using std::size_t; template <char I> struct index { }; struct a { void operator()(size_t const &) { } }; struct b { template <char I> void operator()(index<I> const &) { } }; struct c: public a, public b { }; int main (int argc, char const *argv[]) { c vc; vc(1); return 0; } and give me the following error: main.cpp: In function ‘int main(int, const char**)’: main.cpp:22: error: request for member ‘operator()’ is