-
June 14th, 2019, 09:02 AM
#1
Circular Buffer POP()
Hi,
I have Circular buffer with POP() method,
If the buffer is empty then i will return 0,but when i try to use buffer with other object which is not convertible to zero then it will fail to compile
thanks in advance
-
June 14th, 2019, 10:45 AM
#2
Re: Circular Buffer POP()
If empty, you could raise an exception rather than try to return something.
Another way would be to return a type optional See https://en.cppreference.com/w/cpp/utility/optional (only c++17)
and if the buffer is empty return optional without a value.
You could also just ignore trying to pop on an empty buffer and let an error occur - make it the users responsibility to test that the buffer is not empty before trying to pop.
If this is a templated class/function with the underlying data of type T, then if the buffer is empty you could just return T {} - which is a default object for type T (0 for int etc) (assuming T allows a default).
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)
-
June 14th, 2019, 02:41 PM
#3
Re: Circular Buffer POP()
Yet another option you sometimes encounter is this call signature,
Code:
bool try_pop(T& value) { // non-blocking pop
A variable of type T is passed to try_pop by reference. If try_pop returns true the variable has been assigned an object from the buffer, otherwise the buffer was empty (leaving the content of the variable undefined).
Last edited by wolle; June 16th, 2019 at 03:53 AM.
-
June 17th, 2019, 08:28 AM
#4
Re: Circular Buffer POP()
Originally Posted by 2kaud
If empty, you could raise an exception rather than try to return something.
Another way would be to return a type optional See https://en.cppreference.com/w/cpp/utility/optional (only c++17)
and if the buffer is empty return optional without a value.
You could also just ignore trying to pop on an empty buffer and let an error occur - make it the users responsibility to test that the buffer is not empty before trying to pop.
If this is a templated class/function with the underlying data of type T, then if the buffer is empty you could just return T {} - which is a default object for type T (0 for int etc) (assuming T allows a default).
Is there any other method other that returning T{}?
-
June 17th, 2019, 10:14 AM
#5
Re: Circular Buffer POP()
Last edited by 2kaud; June 17th, 2019 at 10:18 AM.
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)
-
June 17th, 2019, 11:22 AM
#6
Re: Circular Buffer POP()
Originally Posted by Raj90
Is there any other method other that returning T{}?
You can use the call signature I suggested in #3.
-
June 17th, 2019, 11:43 AM
#7
Re: Circular Buffer POP()
Basically for dealing with this type of situation you have 3 options:
1) Remove the test code and do nothing. The calling code will probably cause a run-time memory error. This is common in c/c++ code where bounds are not usually checked for speed performance reasons. That's why with say vector[] and vector at, [] doesn't do any checks whereas at does - but is slower. You could have another function, say empty(), which returns true if the buffer is empty and false if not. This again is a common practice.
2) Raise an exception. Again this is common in c++. The exception would be out_of_range.
3) Indicate an error via a return value. This is not considered particularly good c++ practice. However, if you really want to do this you have a few options:
a) return T{} - default value for type T
b) have a return type of optional<T> and for error return nullopt otherwise return the value.
c) return a type pair<bool, T> where bool indicates whether the value of T is valid or not.
d) As per Wolle's post #3 - return a bool and have the argument to pop() a non-const reference for the value if no issue arises.
How you want to do this is really down to you and what interfaces you are providing - or is the interface given? is this a homework exercise?
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)
-
June 18th, 2019, 05:42 AM
#8
Re: Circular Buffer POP()
Originally Posted by 2kaud
Basically for dealing with this type of situation you have 3 options:
1) Remove the test code and do nothing. The calling code will probably cause a run-time memory error. This is common in c/c++ code where bounds are not usually checked for speed performance reasons. That's why with say vector[] and vector at, [] doesn't do any checks whereas at does - but is slower. You could have another function, say empty(), which returns true if the buffer is empty and false if not. This again is a common practice.
2) Raise an exception. Again this is common in c++. The exception would be out_of_range.
3) Indicate an error via a return value. This is not considered particularly good c++ practice. However, if you really want to do this you have a few options:
a) return T{} - default value for type T
b) have a return type of optional<T> and for error return nullopt otherwise return the value.
c) return a type pair<bool, T> where bool indicates whether the value of T is valid or not.
d) As per Wolle's post #3 - return a bool and have the argument to pop() a non-const reference for the value if no issue arises.
How you want to do this is really down to you and what interfaces you are providing - or is the interface given? is this a homework exercise?
I can't raise an exception, because we don't use them
return nullopt--> this is only used in c++17,as we are using c++11?
Is there any other way to solve?
-
June 18th, 2019, 05:50 AM
#9
Re: Circular Buffer POP()
See d) above and Wolle's post #3
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)
-
June 18th, 2019, 06:09 AM
#10
Re: Circular Buffer POP()
Originally Posted by wolle
You can use the call signature I suggested in #3.
Can you please provide me the code snippet for this?
-
June 18th, 2019, 06:39 AM
#11
Re: Circular Buffer POP()
What's your current pop() code?
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)
-
June 18th, 2019, 08:34 AM
#12
Re: Circular Buffer POP()
Originally Posted by 2kaud
What's your current pop() code?
Code:
T pop()
{
if (isEmpty())
{
return T{};
}
size -= 1;
int index = tail;
updateIndex(tail);
return buffer[index];
}
Last edited by 2kaud; June 18th, 2019 at 09:34 AM.
Reason: Added code tags
-
June 18th, 2019, 09:38 AM
#13
Re: Circular Buffer POP()
[When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.]
For Wolle's suggestion consider:
Code:
bool try_pop(T& value)
{
if (isEmpty())
return false;
--size;
int index = tail;
updateIndex(tail);
value = buffer[index];
return true;
}
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)
-
June 19th, 2019, 02:55 AM
#14
Re: Circular Buffer POP()
Originally Posted by 2kaud
[When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.]
For Wolle's suggestion consider:
Code:
bool try_pop(T& value)
{
if (isEmpty())
return false;
--size;
int index = tail;
updateIndex(tail);
value = buffer[index];
return true;
}
Return type of method should not change it should return T type and it should not pass any arguments?
With this requirement is there any idea for the above issue?
-
June 19th, 2019, 03:28 AM
#15
Re: Circular Buffer POP()
You have already been given the possibilities.
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
|