Author Topic: std::endl : wrong completion  (Read 14243 times)

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5267
std::endl : wrong completion
« on: August 26, 2010, 12:07:14 pm »
For the first time (I think), CB::CC allowed me to complete std::endl. But it putted :
Code: [Select]
std::endl()
The "()" shouldn't be there.

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: std::endl : wrong completion
« Reply #1 on: August 26, 2010, 03:31:14 pm »
it is something related to auto assert "()",can you turn off this,and try again.if it work ,there maybe is something wrong with the parse for token "endl".
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5267
Re: std::endl : wrong completion
« Reply #2 on: August 26, 2010, 10:02:13 pm »
note that this does not happen during the completion of std::cout !

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5295
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: std::endl : wrong completion
« Reply #3 on: August 27, 2010, 01:48:44 am »
note that this does not happen during the completion of std::cout !
simple answer:
cout is regarded as a variable, and endl is regarded as a function( several token named "endl" exist under std namespace, but all of them were functions). see the image below:





This is due some parser problem... let me check it if I have time. :D

If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: std::endl : wrong completion
« Reply #4 on: August 27, 2010, 05:57:10 am »
Let's see iostream file (LINE: 536)
Quote
// [27.6.2.7] standard basic_ostream manipulators
  /**
   *  @brief  Write a newline and flush the stream.
   *
   *  This manipulator is often mistakenly used when a simple newline is
   *  desired, leading to poor buffering performance.  See
   *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
   *  on this subject.
  */
  template<typename _CharT, typename _Traits>
    inline basic_ostream<_CharT, _Traits>&
    endl(basic_ostream<_CharT, _Traits>& __os)
    { return flush(__os.put(__os.widen('\n'))); }
This is a function!

But in:ostream.tcc (LINE: 361 or 384)
Quote
#if _GLIBCXX_EXTERN_TEMPLATE
  extern template class basic_ostream<char>;
 extern template ostream& endl(ostream&);
  extern template ostream& ends(ostream&);
Here is a variable.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5295
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: std::endl : wrong completion
« Reply #5 on: August 27, 2010, 06:55:04 am »
But in:ostream.tcc (LINE: 361 or 384)
Quote
#if _GLIBCXX_EXTERN_TEMPLATE
  extern template class basic_ostream<char>;
 extern template ostream& endl(ostream&);
  extern template ostream& ends(ostream&);
Here is a variable.

our parser just recognize this "endl" as a function, because it has some grammar mode:
Code: [Select]
AAAA BBBB();
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: std::endl : wrong completion
« Reply #6 on: August 27, 2010, 07:06:16 am »
But in:ostream.tcc (LINE: 361 or 384)
Quote
#if _GLIBCXX_EXTERN_TEMPLATE
  extern template class basic_ostream<char>;
 extern template ostream& endl(ostream&);
  extern template ostream& ends(ostream&);
Here is a variable.

our parser just recognize this "endl" as a function, because it has some grammar mode:
Code: [Select]
AAAA BBBB();

Looks can only judge of the special?
Quote
Index: src/plugins/codecompletion/codecompletion.cpp
===================================================================
--- src/plugins/codecompletion/codecompletion.cpp   (revision 6543)
+++ src/plugins/codecompletion/codecompletion.cpp   (working copy)
@@ -790,7 +790,8 @@
                 items.Add(tmp);
                 if (autoAddParentheses && token->m_TokenKind == tkFunction)
                 {
-                    m_SearchItem[token->m_Name] = token->m_Args.size() - 2;
+                    if (token->m_Name != _T("endl"))
+                        m_SearchItem[token->m_Name] = token->m_Args.size() - 2;
                 }
                 if (token->m_TokenKind == tkNamespace && token->m_Aliases.size())
                 {

[attachment deleted by admin]
« Last Edit: August 27, 2010, 07:12:49 am by Loaden »

Offline eranif

  • Regular
  • ***
  • Posts: 254
Re: std::endl : wrong completion
« Reply #7 on: August 27, 2010, 07:55:16 am »
std::endl *is* a function.

When people are typing std::endl they are actually passing the pointer of the function to the 'operator <<' of class basic_ostream

Code: [Select]
__ostream_type&
      operator<<(__ostream_type& (*__pf)(__ostream_type&))

The same is true for all other similar methods, like std::hex, std::oct, std::dec etc
So if you are going to special handle the 'endl' case, make sure you handle the other methods as well (hex, dec, oct etc.)

Eran

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5295
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: std::endl : wrong completion
« Reply #8 on: August 27, 2010, 07:58:05 am »
std::endl *is* a function.

When people are typing std::endl they are actually passing the pointer of the function to the 'operator <<' of class basic_ostream

Code: [Select]
__ostream_type&
      operator<<(__ostream_type& (*__pf)(__ostream_type&))

The same is true for all other similar methods, like std::hex, std::oct, std::dec etc
So if you are going to special handle the 'endl' case, make sure you handle the other methods as well (hex, dec, oct etc.)

Eran


Thanks eran for your help.
So, my personal question is: how does Codelite deal with this kind of problem??
thanks.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline eranif

  • Regular
  • ***
  • Posts: 254
Re: std::endl : wrong completion
« Reply #9 on: August 27, 2010, 08:02:19 am »
Quote
So, my personal question is: how does Codelite deal with this kind of problem??
I am not :D

The same as codeblocks:
when user types std::endl -> codelite adds the () and shows the function tip. I never find it a problem since in codelite if I delete the open brace, the closing brace is also deleted

Eran

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5295
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: std::endl : wrong completion
« Reply #10 on: August 27, 2010, 08:09:46 am »
Quote
So, my personal question is: how does Codelite deal with this kind of problem??
I am not :D

OK, Now we have two ways:
One is like codelite does
Quote
The same as codeblocks:
when user types std::endl -> codelite adds the () and shows the function tip. I never find it a problem since in codelite if I delete the open brace, the closing brace is also deleted
The other is special handling like: (endl, hex, dec, oct etc)

thanks. :D
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5295
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: std::endl : wrong completion
« Reply #11 on: August 27, 2010, 08:11:26 am »
maybe, a better way is :

checking the <<" can accept a function pointer, then we can avoid adding "()" in this case. But it is too complex  :D
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9609
Re: std::endl : wrong completion
« Reply #12 on: August 27, 2010, 08:12:57 am »
Looks can only judge of the special?
No good. Imagine you have a local variable / method with this name than endl has a different meaning and appending the brackets might be desired. In addition: actually this codecompletion should not have any language specific elements at all... ;-)
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: std::endl : wrong completion
« Reply #13 on: August 27, 2010, 08:35:57 am »
Looks can only judge of the special?
No good. Imagine you have a local variable / method with this name than endl has a different meaning and appending the brackets might be desired. In addition: actually this codecompletion should not have any language specific elements at all... ;-)
Well, we still remain the same.

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: std::endl : wrong completion
« Reply #14 on: August 27, 2010, 08:36:31 am »
maybe, a better way is :

checking the <<" can accept a function pointer, then we can avoid adding "()" in this case. But it is too complex  :D
I think this is not possible!