-
February 7th, 2021, 09:19 AM
#1
:: operator new()
I just came across this code in a template class:-
Code:
pointer allocate (size_type n, void* hint = 0)
{
if ((pointer)&_buf + stack_capacity >= _ptr + n) {
pointer rv = _ptr;
_ptr += n;
return rv;
} else {
return static_cast<pointer> (::operator new (n * sizeof (T)));
}
}
I just wondered what's the significance of :: operator in front of the call to new()
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
February 7th, 2021, 12:57 PM
#2
Re: :: operator new()
:: usually means global. Just to distinguish from the possible local definition of the same operator or method in a class.
Victor Nijegorodov
-
February 7th, 2021, 01:50 PM
#3
Re: :: operator new()
https://en.cppreference.com/w/cpp/me...w/operator_new
section "Class-specific overloads":
"If defined, these allocation functions are called by new-expressions to allocate memory for single objects and arrays of this class, unless the new expression used the form ::new which bypasses class-scope lookup."
-
February 8th, 2021, 04:29 AM
#4
Re: :: operator new()
Code:
return static_cast<pointer> (::operator new (n * sizeof (T)));
operator new (<size>) allocates memory and returns a type void* but does not call any constructor. :: just means the global namespace version.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|