Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode C++ development, clarification needed

Tags:

c++

xcode

I absolutely love the way Xcode offers insight into possible available member functions of the language and would prefer to use it relative to, say, text mate, if not for an oddity i noticed today.

When string s = "Test string"; the only available substr signature is as shown

enter image description here

From what i understand however, and what i see online the signature should be

string substr ( size_t pos = 0, size_t n = npos ) const;

Indeed s.substr(1,2); is both understood and works in Xcode.

Why does it not show when i try to method complete? (Ctrl-Space)

like image 732
James Raitsev Avatar asked Jan 16 '12 21:01

James Raitsev


1 Answers

Xcode is performing the completion correctly, but it's not what you expect. You've actually answered the question yourself unknowingly. The function signature for string's substr() method, just as you said, is:

string substr ( size_t pos = 0, size_t n = npos ) const;

All arguments to substr() have default assignments, therefore to Xcode, s.substr() (with no arguments) is the valid code completion to insert because it's really s.substr(0, s.npos). You can confirm this with any number of standard C++ functions with default arguments. The easiest place to see this is with any STL container constructor.

Take for instance a vector. We all know that vectors can take an Allocator, but the default argument assigned Allocator is "good enough" for most casual uses. Sure enough, two of the signatures for vector constructors are:

explicit vector ( const Allocator& = Allocator() );
explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

In both cases, the Allocator argument has a default assignment, and in the second, the T default value has a default assignment. Now, take a look at what Xcode suggests when constructing a vector:

Xcode 4.2.1 Code Completions for vector constructor

The suggestion with no argument list is actually the constructor that takes just an Allocator. The suggestion that takes just a size_type is actually the constructor that takes a size_type, T, and Allocator.

Depending on how you think about this, it may or may not be an Xcode bug. Ideally, you want to see completions with default arguments for simpler functions like substr(), but for STL container constructors, you probably almost never want to see them. Perhaps it could be an option, but I wouldn't expect to see this corrected. I'd happily dup a radar with you though.

like image 154
greg Avatar answered Oct 28 '22 23:10

greg