Search Results

Search found 6 results on 1 pages for 'krao'.

Page 1/1 | 1 

  • C++: Check istream has non-space, non-tab, non-newline characters left without extracting chars

    - by KRao
    I am reading a std::istream and I need to verify without extracting characters that: 1) The stream is not "empty", i.e. that trying to read a char will not result in an fail state (solved by using peek() member function and checking fail state, then setting back to original state) 2) That among the characters left there is at least one which is not a space, a tab or a newline char. The reason for this is, is that I am reading text files containing say one int per line, and sometimes there may be extra spaces / new-lines at the end of the file and this causes issues when I try get back the data from the file to a vector of int. A peek(int n) would probably do what I need but I am stuck with its implementation. I know I could just read istream like: while (myInt << myIstream) {...} //Will fail when I am at the end but the same check would fail for a number of different conditions (say I have something which is not an int on some line) and being able to differentiate between the two reading errors (unexpected thing, nothing left) would help me to write more robust code, as I could write: while (something_left(myIstream)) { myInt << myIstream; if (myStream.fail()) {...} //Horrible things happened } Thank you!

    Read the article

  • C++ MPL or_, and_ implementations

    - by KRao
    Hi, I am trying to read the boost headers to figure out how they managed to implement the or_<...> and and_<...> metafunctions so that: 1) They can have an arbitrary number of arguments (ok, say up to 5 arguments) 2) They have short circuit behavior, for example: or_<false_,true_,...> does not instantiate whatever is after true_ (so it can also be declared but not defined) Unfortunately the pre-processor metaprogramming is making my task impossible for me :P Thank you in advance for any help/suggestion.

    Read the article

  • C++ templated factory constructor/de-serialization

    - by KRao
    Hi, I was looking at the boost serialization library, and the intrusive way to provide support for serialization is to define a member function with signature (simplifying): class ToBeSerialized { public: //Define this to support serialization //Notice not virtual function! template<class Archive> void serialize(Archive & ar) {.....} }; Moreover, one way to support serilization of derived class trough base pointers is to use a macro of the type: //No mention to the base class(es) from which Derived_class inherits BOOST_CLASS_EXPORT_GUID(Derived_class, "derived_class") where Derived_class is some class which is inheriting from a base class, say Base_class. Thanks to this macro, it is possible to serialize classes of type Derived_class through pointers to Base_class correctly. The question is: I am used in C++ to write abstract factories implemented through a map from std::string to (pointer to) functions which return objects of the desired type (and eveything is fine thanks to covariant types). Hover I fail to see how I could use the above non-virtual serialize template member function to properly de-serialize (i.e. construct) an object without knowing its type (but assuming that the type information has been stored by the serializer, say in a string). What I would like to do (keeping the same nomenclature as above) is something like the following: XmlArchive xmlArchive; //A type or archive xmlArchive.open("C:/ser.txt"); //Contains type information for the serialized class Base_class* basePtr = Factory<Base_class>::create("derived_class",xmlArchive); with the function on the righ-hand side creating an object on the heap of type Derived_class (via default constructor, this is the part I know how to solve) and calling the serialize function of xmlArchive (here I am stuck!), i.e. do something like: Base_class* Factory<Base_class>::create("derived_class",xmlArchive) { Base_class* basePtr = new Base_class; //OK, doable, usual map string to pointer to function static_cast<Derived_class*>( basePtr )->serialize( xmlArchive ); //De-serialization, how????? return basePtr; } I am sure this can be done (boost serialize does it but its code is impenetrable! :P), but I fail to figure out how. The key problem is that the serialize function is a template function. So I cannot have a pointer to a generic templated function. As the point in writing the templated serialize function is to make the code generic (i.e. not having to re-write the serialize function for different Archivers), it does not make sense then to have to register all the derived classes for all possible archive types, like: MY_CLASS_REGISTER(Derived_class, XmlArchive); MY_CLASS_REGISTER(Derived_class, TxtArchive); ... In fact in my code I relies on overloading to get the correct behaviour: void serialize( XmlArchive& archive, Derived_class& derived ); void serialize( TxtArchive& archive, Derived_class& derived ); ... The key point to keep in mind is that the archive type is always known, i.e. I am never using runtime polymorphism for the archive class...(again I am using overloading on the archive type). Any suggestion to help me out? Thank you very much in advance! Cheers

    Read the article

  • C++ allocate objects on heap of base class with protected constructors via inheritance

    - by KRao
    I have a class with protected constructor: class B { protected: B(){}; }; Now I derive from it and define two static functions and I manage to actually create objects of the class B, but not on the heap: class A : public B { public: static B createOnStack() {return B();} //static B* createOnHeap() {return new B;} //Compile time Error on VS2010 }; B b = A::createOnStack(); //This works on VS2010! The question is: 1) Is VS2010 wrong in allowing the first case? 2) Is it possible to create objects of B without modifying B in any way (no friendship and no extra functions). I am asking, because it is possible to make something similar when dealing with instances of B and its member functions, see: http://accu.org/index.php/journals/296 Thank you in advance for any suggestion! Kind regards

    Read the article

  • Disallow taking pointer/reference to const to a temporary object in C++ (no C++0X)

    - by KRao
    Hi, I am faced with the following issue. Consider the following class: //Will be similar to bost::reference_wrapper template<class T> class Ref { public: explicit Ref(T& t) : m_ptr(&t) {} private: T* m_ptr; }; and this function returning a double double fun() {return 1.0;} If we now have double x = 1.0; const double xc = 1.0; Ref<double> ref1(x); //OK Ref<const double> refc1(cx); //OK good so far, however: //Ref<double> ref2( fun() ); //Fails as I want it to Ref<const double> refc2( fun() ); //Works but I would like it not to Is there a way to modify Ref (the way you prefer) but not the function fun, so that the last line returns a compile-time error? Please notice you can modify the constructor signature (as long as I am able to initialise the Ref as intended). Thank you in advance for your help!

    Read the article

  • Obtain container type from (its) iterator type in C++ (STL)

    - by KRao
    It is easy given a container to get the associated iterators, example: std::vector<double>::iterator i; //An iterator to a std::vector<double> I was wondering if it is possible, given an iterator type, to deduce the type of the "corresponding container" (here I am assuming that for each container there is one and only one (non-const) iterator). More precisely, I would like a template metafunction that works with all STL containers (without having to specialize it manually for each single container) such that, for example: ContainerOf< std::vector<double>::iterator >::type evaluates to std::vector<double> Is it possible? If not, why? Thank you in advance for any help!

    Read the article

1