An exception-less wrapper for std::stoi
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
std::stoi
may throw exceptions so it needs to be surrounded by try/catch.
In applications where std::stoi
may be used frequently, it could be useful to have a wrapper.
Is this good practice?
int _stoi(std::string str, int* p_value)
// wrapping std::stoi because it may throw an exception
try
*p_value = std::stoi(str);
return 0;
catch (const std::invalid_argument& ia)
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
catch (const std::out_of_range& oor)
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
catch (const std::exception& e)
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
c++ parsing error-handling integer wrapper
New contributor
add a comment |Â
up vote
1
down vote
favorite
std::stoi
may throw exceptions so it needs to be surrounded by try/catch.
In applications where std::stoi
may be used frequently, it could be useful to have a wrapper.
Is this good practice?
int _stoi(std::string str, int* p_value)
// wrapping std::stoi because it may throw an exception
try
*p_value = std::stoi(str);
return 0;
catch (const std::invalid_argument& ia)
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
catch (const std::out_of_range& oor)
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
catch (const std::exception& e)
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
c++ parsing error-handling integer wrapper
New contributor
std::strtol addresses the problem in a much cleaner way.
â vnp
2 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
std::stoi
may throw exceptions so it needs to be surrounded by try/catch.
In applications where std::stoi
may be used frequently, it could be useful to have a wrapper.
Is this good practice?
int _stoi(std::string str, int* p_value)
// wrapping std::stoi because it may throw an exception
try
*p_value = std::stoi(str);
return 0;
catch (const std::invalid_argument& ia)
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
catch (const std::out_of_range& oor)
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
catch (const std::exception& e)
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
c++ parsing error-handling integer wrapper
New contributor
std::stoi
may throw exceptions so it needs to be surrounded by try/catch.
In applications where std::stoi
may be used frequently, it could be useful to have a wrapper.
Is this good practice?
int _stoi(std::string str, int* p_value)
// wrapping std::stoi because it may throw an exception
try
*p_value = std::stoi(str);
return 0;
catch (const std::invalid_argument& ia)
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
catch (const std::out_of_range& oor)
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
catch (const std::exception& e)
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
c++ parsing error-handling integer wrapper
c++ parsing error-handling integer wrapper
New contributor
New contributor
edited 1 hour ago
200_success
126k14147408
126k14147408
New contributor
asked 4 hours ago
Sparkler
1061
1061
New contributor
New contributor
std::strtol addresses the problem in a much cleaner way.
â vnp
2 hours ago
add a comment |Â
std::strtol addresses the problem in a much cleaner way.
â vnp
2 hours ago
std::strtol addresses the problem in a much cleaner way.
â vnp
2 hours ago
std::strtol addresses the problem in a much cleaner way.
â vnp
2 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
This wrapper effectively removes some of the available functionality from std::stoi()
because its signature is
int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
Because your wrapper does not allow a pos
or base
argument you cannot use it to give you the number of characters processed (with pos
) nor to convert using a different base. std::stoi()
provides default values for these arguments so you'd only have to provide them if you need the non-default behavior.
Also, you don't take the std::string
argument by const reference like std::stoi()
-- the string argument is probably not too expensive to copy in this context but why not accept the argument the same way std::stoi()
does?
For completeness, I would also implement the overload of std::stoi()
which accepts a std::wstring
(and possibly std::stol()
and std::stoll()
.
I would also avoid the leading underscore, as identifiers with a leading underscore are reserved.
With these suggestions the wrapper would be implemented as
int stoi(const std::string& str, int* p_value, std::size_t* pos = 0, int base = 10)
// wrapping std::stoi because it may throw an exception
try
*p_value = std::stoi(str, pos, base);
return 0;
catch (const std::invalid_argument& ia)
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
catch (const std::out_of_range& oor)
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
catch (const std::exception& e)
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
add a comment |Â
up vote
1
down vote
int _stoi(std::string str, int* p_value) {
Identifiers that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
I would expect the signature of your _stoi
to match that of the std::stoi
you are wrapping. So you should take the std::string
by reference-to-const
, take an in-out parameter to indicate how much of str
was processed, and the base you are converting to.
try
*p_value = std::stoi(str);
return 0;
catch (...)
return -3;
Instead of returning integers that represent error-codes, leverage the type-system. There are a class of types called "either" types that let you return a result or an enumerated error. There are also variant types that let you return one of multiple types of return or error values.
If you plan on supporting the full set of std::stoX
family of functions and their overloads between std::string
and std::wstring
, I urge you to look at lippincott functions as well as variadic argument passing.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
This wrapper effectively removes some of the available functionality from std::stoi()
because its signature is
int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
Because your wrapper does not allow a pos
or base
argument you cannot use it to give you the number of characters processed (with pos
) nor to convert using a different base. std::stoi()
provides default values for these arguments so you'd only have to provide them if you need the non-default behavior.
Also, you don't take the std::string
argument by const reference like std::stoi()
-- the string argument is probably not too expensive to copy in this context but why not accept the argument the same way std::stoi()
does?
For completeness, I would also implement the overload of std::stoi()
which accepts a std::wstring
(and possibly std::stol()
and std::stoll()
.
I would also avoid the leading underscore, as identifiers with a leading underscore are reserved.
With these suggestions the wrapper would be implemented as
int stoi(const std::string& str, int* p_value, std::size_t* pos = 0, int base = 10)
// wrapping std::stoi because it may throw an exception
try
*p_value = std::stoi(str, pos, base);
return 0;
catch (const std::invalid_argument& ia)
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
catch (const std::out_of_range& oor)
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
catch (const std::exception& e)
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
add a comment |Â
up vote
2
down vote
This wrapper effectively removes some of the available functionality from std::stoi()
because its signature is
int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
Because your wrapper does not allow a pos
or base
argument you cannot use it to give you the number of characters processed (with pos
) nor to convert using a different base. std::stoi()
provides default values for these arguments so you'd only have to provide them if you need the non-default behavior.
Also, you don't take the std::string
argument by const reference like std::stoi()
-- the string argument is probably not too expensive to copy in this context but why not accept the argument the same way std::stoi()
does?
For completeness, I would also implement the overload of std::stoi()
which accepts a std::wstring
(and possibly std::stol()
and std::stoll()
.
I would also avoid the leading underscore, as identifiers with a leading underscore are reserved.
With these suggestions the wrapper would be implemented as
int stoi(const std::string& str, int* p_value, std::size_t* pos = 0, int base = 10)
// wrapping std::stoi because it may throw an exception
try
*p_value = std::stoi(str, pos, base);
return 0;
catch (const std::invalid_argument& ia)
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
catch (const std::out_of_range& oor)
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
catch (const std::exception& e)
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
add a comment |Â
up vote
2
down vote
up vote
2
down vote
This wrapper effectively removes some of the available functionality from std::stoi()
because its signature is
int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
Because your wrapper does not allow a pos
or base
argument you cannot use it to give you the number of characters processed (with pos
) nor to convert using a different base. std::stoi()
provides default values for these arguments so you'd only have to provide them if you need the non-default behavior.
Also, you don't take the std::string
argument by const reference like std::stoi()
-- the string argument is probably not too expensive to copy in this context but why not accept the argument the same way std::stoi()
does?
For completeness, I would also implement the overload of std::stoi()
which accepts a std::wstring
(and possibly std::stol()
and std::stoll()
.
I would also avoid the leading underscore, as identifiers with a leading underscore are reserved.
With these suggestions the wrapper would be implemented as
int stoi(const std::string& str, int* p_value, std::size_t* pos = 0, int base = 10)
// wrapping std::stoi because it may throw an exception
try
*p_value = std::stoi(str, pos, base);
return 0;
catch (const std::invalid_argument& ia)
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
catch (const std::out_of_range& oor)
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
catch (const std::exception& e)
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
This wrapper effectively removes some of the available functionality from std::stoi()
because its signature is
int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
Because your wrapper does not allow a pos
or base
argument you cannot use it to give you the number of characters processed (with pos
) nor to convert using a different base. std::stoi()
provides default values for these arguments so you'd only have to provide them if you need the non-default behavior.
Also, you don't take the std::string
argument by const reference like std::stoi()
-- the string argument is probably not too expensive to copy in this context but why not accept the argument the same way std::stoi()
does?
For completeness, I would also implement the overload of std::stoi()
which accepts a std::wstring
(and possibly std::stol()
and std::stoll()
.
I would also avoid the leading underscore, as identifiers with a leading underscore are reserved.
With these suggestions the wrapper would be implemented as
int stoi(const std::string& str, int* p_value, std::size_t* pos = 0, int base = 10)
// wrapping std::stoi because it may throw an exception
try
*p_value = std::stoi(str, pos, base);
return 0;
catch (const std::invalid_argument& ia)
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
catch (const std::out_of_range& oor)
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
catch (const std::exception& e)
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
edited 3 hours ago
answered 3 hours ago
Null
9032920
9032920
add a comment |Â
add a comment |Â
up vote
1
down vote
int _stoi(std::string str, int* p_value) {
Identifiers that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
I would expect the signature of your _stoi
to match that of the std::stoi
you are wrapping. So you should take the std::string
by reference-to-const
, take an in-out parameter to indicate how much of str
was processed, and the base you are converting to.
try
*p_value = std::stoi(str);
return 0;
catch (...)
return -3;
Instead of returning integers that represent error-codes, leverage the type-system. There are a class of types called "either" types that let you return a result or an enumerated error. There are also variant types that let you return one of multiple types of return or error values.
If you plan on supporting the full set of std::stoX
family of functions and their overloads between std::string
and std::wstring
, I urge you to look at lippincott functions as well as variadic argument passing.
add a comment |Â
up vote
1
down vote
int _stoi(std::string str, int* p_value) {
Identifiers that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
I would expect the signature of your _stoi
to match that of the std::stoi
you are wrapping. So you should take the std::string
by reference-to-const
, take an in-out parameter to indicate how much of str
was processed, and the base you are converting to.
try
*p_value = std::stoi(str);
return 0;
catch (...)
return -3;
Instead of returning integers that represent error-codes, leverage the type-system. There are a class of types called "either" types that let you return a result or an enumerated error. There are also variant types that let you return one of multiple types of return or error values.
If you plan on supporting the full set of std::stoX
family of functions and their overloads between std::string
and std::wstring
, I urge you to look at lippincott functions as well as variadic argument passing.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
int _stoi(std::string str, int* p_value) {
Identifiers that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
I would expect the signature of your _stoi
to match that of the std::stoi
you are wrapping. So you should take the std::string
by reference-to-const
, take an in-out parameter to indicate how much of str
was processed, and the base you are converting to.
try
*p_value = std::stoi(str);
return 0;
catch (...)
return -3;
Instead of returning integers that represent error-codes, leverage the type-system. There are a class of types called "either" types that let you return a result or an enumerated error. There are also variant types that let you return one of multiple types of return or error values.
If you plan on supporting the full set of std::stoX
family of functions and their overloads between std::string
and std::wstring
, I urge you to look at lippincott functions as well as variadic argument passing.
int _stoi(std::string str, int* p_value) {
Identifiers that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
I would expect the signature of your _stoi
to match that of the std::stoi
you are wrapping. So you should take the std::string
by reference-to-const
, take an in-out parameter to indicate how much of str
was processed, and the base you are converting to.
try
*p_value = std::stoi(str);
return 0;
catch (...)
return -3;
Instead of returning integers that represent error-codes, leverage the type-system. There are a class of types called "either" types that let you return a result or an enumerated error. There are also variant types that let you return one of multiple types of return or error values.
If you plan on supporting the full set of std::stoX
family of functions and their overloads between std::string
and std::wstring
, I urge you to look at lippincott functions as well as variadic argument passing.
answered 3 hours ago
Snowhawk
4,69911027
4,69911027
add a comment |Â
add a comment |Â
Sparkler is a new contributor. Be nice, and check out our Code of Conduct.
Sparkler is a new contributor. Be nice, and check out our Code of Conduct.
Sparkler is a new contributor. Be nice, and check out our Code of Conduct.
Sparkler is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206754%2fan-exception-less-wrapper-for-stdstoi%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
std::strtol addresses the problem in a much cleaner way.
â vnp
2 hours ago