Thread: Storing functors?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Storing functors?

    I seem to have hit a bit of a snag again.
    I wanted to make it possible to couple (I think that's the right word?) my input class a little more, as to make it the answer itself, as well as the inputter of that answer.
    To do that, I wanted to add verification as part of the process, as well as the possibility of a custom conversion std::string -> T.
    Basically that would mean taking a functor in the constructor and storing it for later when the ask member function is called. But how would we do this? I cannot store it inside the class if I do not know the type... and to know the type, I would have to include it for the class template, which means explicitly specifying it which defeats the entire purpose...

    Code:
    template<typename Type, typename CharT = StrType_t,
             template<typename> class ValidateFunctor = ValidateDefault,
             template<typename> class Traits = InputTraits
      > class CInput
    The red part highlights the unknown. Taking a class functor is not really what I want. I basically want to make it work with lambda expressions that is coming with C++0x. A sample use of how to use the class would be:

    Code:
    Stuff::Cinput<int> KeyLength(
        "Enter length of the key", [&]
        {
            cout << "Error. Invalid key.";
        },
        [&] (int Key) -> bool
        {
            return (Key >= 2 && Key <= 5);
        });
    An example of how I would like an implementation is:
    Code:
    		void ask(Type& Answer)
    		{
    			Answer = m_Converter();
    			if (! m_Validater(Answer) )
    				m_Error();
    		}
    I simply lack the know-how.
    Last edited by Elysia; 11-12-2008 at 06:15 AM. Reason: Make more readable
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Cstrings in an Array
    By Tien1868 in forum C Programming
    Replies: 4
    Last Post: 07-09-2009, 07:48 PM
  2. Storing Passwords/Encryption
    By Tonto in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2009, 09:05 PM
  3. Storing Objects in txt files?
    By Swerve in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2009, 12:17 PM
  4. Variable Storing in memory
    By karb0noxyde in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 07:31 PM
  5. File Reading and storing to 1 variable
    By Rare177 in forum C Programming
    Replies: 34
    Last Post: 07-13-2004, 12:58 PM