CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2017
    Posts
    50

    Why is C++ STL implementation is written like this

    Why they use some kind of crazy naming convension and lot of underscores?

    Code:
    	// constructors
    	vector_impl(_Mytype_t% _Right)
    		{	// construct by copying _Right
    		for (size_type _Idx = _Buy(_Right.size()); 0 <= --_Idx; )
    			_Myarray[_Idx] = _Mymake_t::make_value(_Right.at(_Idx));
    		_Mysize = _Right.size();
    		}
    
    	explicit vector_impl(size_type _Count)
    		{	// construct from _Count * value_type()
    		_Mysize = _Fill_n(0, _Buy(_Count), value_type());
    		}
    
    	vector_impl(size_type _Count, value_type _Val)
    		{	// construct from _Count * _Val
    		_Mysize = _Fill_n(0, _Buy(_Count), _Val);
    		}
    
    	template<typename _InIt_t>
    		vector_impl(_InIt_t _First, _InIt_t _Last)
    		{	// construct from [_First, _Last)
    		_Construct(_First, _Last, _Iter_category(_First));
    		}
    
    	template<typename _InIt_t>
    		void _Construct(_InIt_t _Count, _InIt_t _Val,
    			_Int_iterator_tag%)
    		{	// initialize with _Count * _Val
    		_Mysize = _Fill_n(0, _Buy((size_type)_Count), (value_type)_Val);
    		}
    
    	template<typename _InIt_t>
    		void _Construct(_InIt_t _First, _InIt_t _Last,
    			input_iterator_tag%)
    		{	// initialize with [_First, _Last), input iterators
    		_Buy(_First != _Last ? 1 : 0);	// buy at least one if non-empty
    		for (; _First != _Last; ++_First)
    			insert_n(size(), 1, (value_type)*_First);
    		}
    
    	template<typename _InIt_t>
    		void _Construct(_InIt_t _First, _InIt_t _Last,
    			forward_iterator_tag%)
    		{	// initialize with [_First, _Last), forward iterators
    		size_type _Size = cliext::distance(_First, _Last);
    
    		_Buy(_Size);
    		for (size_type _Idx = 0; _Idx < _Size; ++_Idx, ++_First)
    			_Myarray[_Idx] = _Mymake_t::make_value(value_type(*_First));
    		_Mysize = _Size;
    		}

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Why is C++ STL implementation is written like this

    Variables starting with _ are reserved for use by the C++ language implementers for library usage. Although legal, you should never yourself use a variable that starts with _.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured