• Advertisement


  • Content count

  • Joined

  • Last visited

Community Reputation

643 Good

About visitor

  • Rank
    Advanced Member
  1. C++ Templates + Inheritence

    As for the first question, you can simply throw in a static assertion (either C++0x, a boost one or home-made) together with the (c++0x / boost) type_trait called is_base_of. #include <type_traits> class Vehicle {}; class Car: public Vehicle {}; template <class T> class VehicleShop { static_assert(std::is_base_of<Car, T>::value, "VehicleShop instantiated for a non-Vehicle"); }; int main() { VehicleShop<Car> a; VehicleShop<int> b; }
  2. typedef inside class

    That's OK. There are two typedeffed names at two different scopes (A::MyType and B::MyType). No overriding involved. Only methods can be overridden.
  3. In principle you can do so by creating a temporary in an expression that is destroyed at the end of the expression and can do something in its destructor. Whether this has any advantage over just typing << std::endl remains a question. #include <fstream> #include <iostream> class log { public: log( void ) { this->streamLog.open( "log.txt" ); } ~log() { this->streamLog.close(); } template <class T> log& operator<<(const T& input ) { this->streamLog << input; return *this; } // to support std::endl etxc log& operator<< (std::ostream& (*f)(std::ostream&)) { f(this->streamLog); return *this; } private: std::ofstream streamLog; }; class log_line { log& logger; public: log_line(log& l): logger(l) {} ~log_line() { logger << " ... complete" << std::endl; } template <class T> log& operator<<( T input ) { logger << input; return logger; } //this could also support manipulators }; int main( void ) { log logger; log_line(logger) << 1 << "st line"; log_line(logger) << 2 << "nd line"; }
  4. No, it doesn't first combine the right-hand values. Which would mean that an operator<< would first have to be invoked on two string literals (no such thing exists). What happens is: mystream << "one" returns a reference to a fstream, and then returned_fstream << "two" is executed.
  5. [C++] - Call func for each variadic term?

    I don't think there's a much simpler way than the second. Your attempts also suffer from the fact that the function calls will occur in an unspecified order, as the arguments to dummy are evaluated.
  6. "Automatic" fill

    Basically you are just trying to use an algorithm for something that it is not supposed for. Why use a O(n*n) algorithm for a O(n) operation? There are many better ways, e.g using a counting_iterator from boost. #include <vector> #include <boost/iterator/counting_iterator.hpp> #include <iostream> #include <algorithm> #include <iterator> int main() { std::vector<int> vec(5); std::copy(boost::make_counting_iterator(1), boost::make_counting_iterator(6), vec.begin()); std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; }
  7. Quote: Except it doesn't, it just means the return type is going to be specified at the end of the prototype and not the beginning: auto foo() -> int {...} Well yes, but that use of auto would be about as meaningful as auto i = int(10); More meaningful use cases would be such where there is something to infer: template <class Range, class Value> auto find(Range& range, const Value& what) -> decltype(range.begin()); //returns Range::iterator or Range::const_iterator depending on constness of Range
  8. Quote: EDIT: Also, auto can be used in function definitions where it means something totally different. One of those places where the lack of consistency in 0x really annoys me... It also means that the return type is going to be inferred, so that doesn't seem particularly inconsistent.
  9. Parameter types

    Are templates out of the question? template <class T> void test_entities_outOfFrame (std::vector <T*> *e, int width, int height) { for (unsigned int index = 0; index < e->size ();) { if ((*e)[index]->get_positionX () < 0) (*e)[index]->set_positionX (width) ; if ((*e)[index]->get_positionX () > width) (*e)[index]->set_positionX (0) ; if ((*e)[index]->get_positionY () < 0) (*e)[index]->set_positionY (height) ; if ((*e)[index]->get_positionY () > height) (*e)[index]->set_positionY (0) ; ++ index ; } } Now vector of any pointer having those methods would go.
  10. Returning std::string / std::vector?

    Quote: He's already posted that he's copying a class data member. How could (N)RVO or move constructors possibly stop a copy from being made? Facepalm. Managed to miss that we are talking about a member and thought this was about introducing a global to "optimize" returning. In this case, what you do depends on what you need. Returning a non-const reference (pointer) means that the caller is able to modify the member of the object. For read-only access, return a const reference. Other options might also be returning begin and end iterators (allowing modification of stored items, but not the vector itself). Returning a copy, however, leaves you more freedom to change the implementation if needed. For example, if it turns out you'd rather need to return a computed result, not the member itself.
  11. Returning std::string / std::vector?

    You might also check if it is in any way expensive to return by value. If the usage is like std::string s = function_returning_string(); std::vector<int> v = function_returning_vector(); then it is possible that all copying will be eliminated by (named) return value optimization. In C++0x it will be guaranteed in all cases that such usage at worst invokes a dirt-cheap move constructor. std::vector<Type> v; // global or data member, NOT bound (local) to the scope of f()! std::vector<Type> &f() { return v; } This global state just makes code more brittle (e.g f cannot be used in multiple threads). If the intended usage is as above (initialize a vector), then if NRVO can eliminate all copying when returning by value, here a copy cannot be avoided?
  12. Quote: You can get rid of the signed integer cast if you're willing to add some additional logic to prevent the underflow case, but the erase is going to remain a little ugly :/ If people weren't that resistent to for(size_t i = elementQueue.size(); i --> 0; ) I think that could become an idiom for reverse iterating. Honestly. :)
  13. List iterator not decrementable (C++)

    So how is MSVC not happy. You are getting runtime errors, because it is checking your iterator usage very rigorously? Anyway, your code is a bit bizarre. Why do you loop over each item in a map, instead of using the find member function (that's what the map was invented for)? The rest of the logic is hard to follow too. What happens if you don't find "level" in the "levelNames"? If the list is guaranteed to contain the name, why do you even need to search for it, if you already know what the next level is going to be. As to the backwards search, why do you have to search again? Don't you just want to look at the previous item (if level is not the first item in the list)? This is how you'd find the level in the map: i = mManagers.find(level); if (i != mManagers.end()) // success This is how you'd find the level in the list: j = std::find(levelNames.begin(), levelNames.end(), level); if (j != levelNames.end()) //success And to access the previous item if (j != levelNames.begin()) { --j; //access previous item now }
  14. If I ever got as far as to make a game with an AI, I would try to avoid hard-coding it in the first place.
  15. template error ..

    I wonder why you wouldn't just use: #include<iostream> #include<vector> using namespace std; template<typename Container> class X { Container myContainer; }; int main() { X<vector<int> > cc; }
  • Advertisement