c++ grammar question

This is a discussion on c++ grammar question within the C++ Programming forums, part of the General Programming Boards category; I am currently validating my c++ parser but i failed to parse c++ header which is cygwin, the file is ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    4

    c++ grammar question

    I am currently validating my c++ parser but i failed to parse c++ header which is cygwin, the file is named "C:\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\bits/basic_string.tcc"

    in that file there is declaration of assign template function:
    Code:
       
    template <class _Arg1, class _Arg2, class _Result>
        struct binary_function
        {
          typedef _Arg1 first_argument_type;   ///< the type of the first argument
                                               ///  (no surprises here)
          typedef _Arg2 second_argument_type;  ///< the type of the second argument
          typedef _Result result_type;         ///< type of the return type
        };
    
      template <class _Tp>
        struct less : public binary_function<_Tp, _Tp, bool>
        {
          bool
          operator()(const _Tp& __x, const _Tp& __y) const
          { return __x < __y; }
        };
    
      template<typename _CharT, typename _Traits, typename _Alloc>
         basic_string<_CharT, _Traits, _Alloc>&
         basic_string<_CharT, _Traits, _Alloc>::
         assign(const _CharT* __s, size_type __n)
         {
           __glibcxx_requires_string_len(__s, __n);
           if (__n > this->max_size())
    	 __throw_length_error(__N("basic_string::assign"));
           if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
    	   || less<const _CharT*>()(_M_data() + this->size(), __s))
    	 return _M_replace_safe(size_type(0), this->size(), __s, __n);
           else
    	 {
    	   // Work in-place
    	   const size_type __pos = __s - _M_data();
    	   if (__pos >= __n)
    	     traits_type::copy(_M_data(), __s, __n);
    	   else if (__pos)
    	     traits_type::move(_M_data(), __s, __n);
    	   _M_rep()->_M_set_sharable();
    	   _M_rep()->_M_length = __n;
    	   _M_data()[__n] = _Rep::_S_terminal;  // grr.
    	   return *this;
    	 }
         }
    My problem is :
    less<const _CharT*>()(__s, _M_data())

    Which i really don't understand, seems like this call explicitly the () operator but I cant find in the c++ iso grammar what are the rule used. Is that code use some gcc extension ?

    any help would be greatly appreciated.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't think it's an extension, it is just calling the operator() with two arguments.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    That was my bet too, but official grammar require operator keyword to be used, something like:
    less<const _CharT*>operator()(__s, _M_data())

    also the name of the class is quite disturbing here, the method is not static thus there should be a object somewhere... or at least use scope operator "::"...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    I read that as creating a temporary less<const _CharT*> object, and then invoking operator() of that temporary object with two arguments.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    dont you need to use new keyword to create object ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    dont you need to use new keyword to create object ?
    Of course not. Perhaps you are getting confused with say, Java?

    If you want an example:
    Code:
    #include <iostream>
    #include <string>
    
    class Greet
    {
    public:
        void operator()(const std::string& name) const
        {
            std::cout << "Hello " << name << "!\n";
        }
    };
    
    int main()
    {
        // Named Greeter.
        Greet greet;
        greet("bpereira"); // Call operator().
    
        // Temporary Greeter.
        Greet()("world"); // Call operator().
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bpereira View Post
    dont you need to use new keyword to create object ?
    Only if you want to create an object on the heap (aka "Free Store" to use Bjarne's nomenclature).

    A temporary object is usually a stack-located object, so it doesn't need "new" to create it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    ok thanks for quick answerer, that fixed my issue, sorry I don't practice c++ often enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21