c++: Try catch on construction of a class with a const member - c++

Consider the following class:
class MyClass
{
private:
const unsigned int num;//num identifies the object. needs to be const
unsigned int checkNum(unsigned int);// verifies that num has a valid value
public:
MyClass(unsigned int n): num(checkNum(n)) {}
};
unsigned int MyClass:checkNum(unsigned int n)
{
if (some_condition)
throw std::invalid_argument("Invalid number");
return n;
}
The difficulty is that the object must be constructed inside a try block because of the range check:
int main()
{
try {
MyClass mc(1000);
}
catch (std::invalid_argument &ia)
{
std::cout << ia.what();
}
return 0;
}
The problem is that mc is then not available outside of the try block.
Possible solutions:
Extend the try block over the whole scope where mc is used. Not practical in many cases.
Don't throw the exception in the constructor, but throwing it afterwards would be too late.
The only acceptable solution I can think of is to use smart pointers to bring the declaration outside of the try block:
int main()
{
std::unique_ptr<MyClass> my_class_ptr;
try {
my_class_ptr = std::make_unique<MyClass>(1000);
}
catch (std::invalid_argument &ia)
{
std::cout << ia.what();
}
return 0;
}
Are there any other/better solutions?

What do you intend to do with mc when its construction was deemed invalid, and "cancelled" via exception?
Having the try extend around the entire scope of the object makes perfect sense.
mc shouldn't be accessible outside of the try block.

In your point 1, "not practical in many cases" is simply false.
A try block is the solution where you want to handle failure.
An alternative is to not handle it.
In other news, in your point 2, "Don't make MyClass::num const. Not good , it supposed to be non-mutable for the duration of the object." is very questionable. It is to some extent subject to personal opinion, but technically there's no problem. Also, there's no connection between the const and the range checking: that hypothesized connection simply does not exist.

You should validate the ID number before constructing the mc object with it. Don't do the validation inside the constructor itself:
class MyClass
{
private:
const unsigned int num;
public:
MyClass(unsigned int n): num(n) {}
};
unsigned int checkNum(unsigned int n)
{
if (some_condition)
throw std::invalid_argument("Invalid number");
return n;
}
int main()
{
unsigned int num;
try {
num = checkNum(some_input);
}
catch (std::invalid_argument &ia)
{
std::cout << ia.what();
return -1;
}
MyClass mc(num);
return 0;
}

You can use a function-try-block to prevent any wished of using mc outside of the try block. : >
int main() try{
return 0;
} catch(...) {
}

Related

std::find return a class that I can't acesses functions

I come from C/C# language and now I'm trying to learn about C++ and his standards functions.
Now, I'm creating a class called IMonsterDead. I will have a std::vector<IMonsterDead*> with N monsters.
Example:
class IMonsterDead {
public:
IMonsterDead(int Id)
{
this->_Id = Id;
}
virtual void OnDead() = 0;
int Id() const {
return _Id;
}
private:
int _Id;
};
One class which implements that class:
class MonsterTest : public IMonsterDead {
public:
MonsterTest(int generId)
: IMonsterDead(generId)
{
}
virtual void OnDead()
{
std::cout << "MonsterTesd died" << std::endl;
}
};
Ok, if I access directly everything works fine. But I'm trying to use std::find.
Full program test:
int main()
{
std::vector<IMonsterDead*> monsters;
for (int i = 0; i < 1000; i++)
{
monsters.emplace_back(new MonsterTest(1000 + i));
}
int id = 1033;
std::vector<IMonsterDead*>::iterator result = std::find(monsters.begin(), monsters.end(), [id]( IMonsterDead const* l) {
return l->Id() == id;
});
if (result == monsters.end())
std::cout << "Not found" << std::endl;
else
{
// Here I want to access OnDead function from result
}
return 0;
}
So I need to access OnDead function from result but I can't. Intellisense doesn't show anything for me. The result exists.
How can I access that function? Have another better way to do that?
You need to use std::find_if() instead of std::find(). std::find() is for finding an element with a specific value, so you have to pass it the actual value to find, not a user_defined predicate. std::find_if() is for finding an element based on a predicate.
Either way, if a match is found, dereferencing the returned iterator will give you a IMonsterDead* pointer (more accurately, it will give you a IMonsterDead*& reference-to-pointer). You need to then dereference that pointer in order to access any members, like OnDead().
You are also leaking memory. You are not delete'ing the objects you new. And when dealing with polymorphic types that get deleted via a pointer to a base class, the base class needs a virtual destructor to ensure all derived destructors get called properly.
With that said, you are clearly using C++11 or later (by the fact that you are using vector::emplace_back()), so you should use C++11 features to help you manage your code better:
You should use std::unique_ptr to wrap your monster objects so you don't need to delete them manually.
You should always use the override keyword when overriding a virtual method, to ensure you override it properly. The compiler can catch more syntax errors when using override than without it.
You should use auto whenever you declare a variable that the compiler can deduce its type for you. Especially useful when dealing with templated code.
Try something more like this:
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
class IMonsterDead {
public:
IMonsterDead(int Id)
: m_Id(Id)
{
}
virtual ~IMonsterDead() {}
virtual void OnDead() = 0;
int Id() const {
return m_Id;
}
private:
int m_Id;
};
class MonsterTest : public IMonsterDead {
public:
MonsterTest(int generId)
: IMonsterDead(generId)
{
}
void OnDead() override
{
std::cout << "MonsterTest died" << std::endl;
}
};
int main()
{
std::vector<std::unique_ptr<IMonsterDead>> monsters;
for (int i = 0; i < 1000; i++)
{
// using emplace_back() with a raw pointer risks leaking memory
// if the emplacement fails, so push a fully-constructed
// std::unique_ptr instead, to maintain ownership at all times...
monsters.push_back(std::unique_ptr<IMonsterDead>(new MonsterTest(1000 + i)));
// or:
// std::unique_ptr<IMonsterDead> monster(new MonsterTest(1000 + i));
// monsters.push_back(std::move(monster));
// or, if you are using C++14 or later:
// monsters.push_back(std::make_unique<MonsterTest>(1000 + i));
}
int id = 1033;
auto result = std::find_if(monsters.begin(), monsters.end(),
[id](decltype(monsters)::value_type &l) // or: (decltype(*monsters.begin()) l)
{
return (l->Id() == id);
}
// or, if you are using C++14 or later:
// [id](auto &l) { return (l->Id() == id); }
);
if (result == monsters.end())
std::cout << "Not found" << std::endl;
else
{
auto &monster = *result; // monster is 'std::unique_ptr<IMonsterDead>&'
monster->OnDead();
}
return 0;
}
Iterators are an interesting abstraction, in this case to be reduced to pointers.
Either you receive the pointer to the element or you get an invalid end.
You can use it as a pointer: (*result)->func();
You can also use it to create a new variable:
IMonsterDead &m = **result;
m.func();
This should give the same assembly, both possible.

Initializing object which throws exceptions

I have class A, which has constructor A(string filepath)
class A {
public:
A(string filePath);
private:
A();
}
I need to initialize A, and use it later. Constructor A throws exceptions.
int main() {
A a;
try {
a = A("./my/file/path");
} catch (exception e) {
return 1;
}
// Use a
return 0;
}
What would be the most elegant solution? (I dont want to have something like init function, because it makes it possible to have empty A object.)
The most elegant solution is probably to move all your logic which uses a into a function
int use_a(A &a);
int main() {
try {
A a("./my/file/path");
// if we reach here, no exception was thrown
// hence a is safe to use
return use_a(a);
} catch (exception e) {
// if we reach here a never really existed
return 1;
}
}
You're correct to prefer avoiding two-phase initialization - this way use_a can implicitly rely on a being safe to use.
The most elegant solution is:
int main() {
try {
A a("...");
// use a
} catch (const std::exception& e) {
// log e
return 1;
}
return 0;
}
If you don't want empty A objects consider deleting the default constructor. Note that this makes using some stl containers more difficult, while ensuring no invalid object exists.
In addition to that i think using throw in constructors very much allowed. If you don't want to do that consider the noexcept keyword. Note that throws in destructors can cause problems and are usually avoided.
As alternative, you might turn the exception in "optional/checkable" construction with std::optional or std::unique_ptr:
std::optional<A> MakeA(const std::filesystem::path& path)
{
try {
return A{path};
} catch (std::exception& e) {
return std::nullopt;
}
}
int main() {
auto a = MakeA("./my/file/path");
if (!a) {
return 1;
}
// Use *a
return 0;
}

Passing function and operator calls in object

I am wanting to make a class which allows me to lock an object from being modified. It would essentially be a template with a boolean specifying the lock state. Since it is a template, I won't know all the methods that can be called on the internal object, so I need a method to pass calls through...
template<class T>
class const_lock
{
public:
const_lock() : my_lock(false) {}
void set_const_lock(bool state) {my_lock = state;}
// HOW TO IMPLEMENT SOMETHING LIKE THESE????
//
template<typename...Args >
auto operatorANY_OPERATOR (Args...args)
{
if(my_lock != false)
throw std::exception("Objected locked to modification");
return my_value.ANY_OPERATOR(args);
}
template<typename...Args >
auto operatorANY_CONST_OPERATOR (Args...args) const
{
return my_value.ANY_CONST_OPERATOR(args);
}
template<typename...Args >
auto ANY_METHOD(Args...args)
{
if(my_lock != false)
throw std::exception("Objected locked to modification");
return my_value.ANY_METHOD(args);
}
template<typename...Args >
auto ANY_CONST_METHOD(Args...args) const
{
return my_value.ANY_CONST_METHOD(args);
}
private:
bool my_lock;
T my_value;
}
int main()
{
const_lock<std::vector<int>> v;
v.push_back(5);
v.push_back(7);
v.set_const_lock(true);
v.push_back(9); // fails compilation
std::cout << v.at(1) << std::endl; // ok
}
Any help would be appreciated. Thanks!
Edit: changed static assert to throw and exception
What you're trying to do looks rather difficult, but more importantly is over-complicated and unnecessary for what you're trying to do.
Essentially what you're trying to do (correct me if I'm wrong) is create a compile time check of whether you are supposed to able to modify an object at a given time. However, c++ already has a built in way of doing this. Simply declare or pass your object as const or const&, and the compiler will not allow you to modify non-mutable parts of the object. When you want to be able to modify it pass it without const. You can even cast it from const& to regular & when you want to go from code where you can't modify it directly to code where you can, though I don't recommend it.
edit: just saw a comment on the question about no reference arrays. Don't worry about that! The standard library has support for reference wrappers which allow you to essentially store references in arrays or anywhere else.
You can make a generic wrapper class that you can forward the function to using a lambda that captures a reference to the internal member. In this example I am just using an if statement to check if it is "locked" and if it is then we just modify a copy.
template<class T>
class const_lock
{
private:
bool my_lock;
mutable T my_value;
public:
const_lock() : my_lock(false) {}
void set_const_lock() { my_lock = true; }
template<typename F>
auto operator()(F f) const -> decltype(f(my_value))
{
if (my_lock)
{
T temp{my_value}; // make a copy
return f(temp);
}
else
return f(my_value); // modify wrraped value
}
};
int main()
{
const_lock<std::string> cl;
cl([](std::string& s) {
s = "foobar";
});
cl([](std::string& s) {
std::cout << s << std::endl;
});
cl.set_const_lock();
cl([](std::string& s) {
s = "we should still be foobar";
});
cl([](std::string& s) {
std::cout << s;
});
}
This is completely unimplementable. A trivial modification of your source code shows why this won't work.
int main()
{
const_lock<std::vector<int>> v;
v.push_back(5);
v.push_back(7);
if (rand() % 2)
v.set_const_lock(true);
v.push_back(9); // fails compilation
std::cout << v.at(1) << std::endl; // ok
}
You need to completely rethink your approach.
Below is an example illustrating what I would be trying to protect against
class Node
{
public:
Node(int id) : my_id(id) {}
// . . .
int id() {return my_id;}
private:
int my_id;
// . . .
};
class Grid
{
public:
Grid() {}
// . . .
void associate(Node* n) { my_nodes.push_back(n); }
private:
// . . .
std::vector<Node*> my_nodes;
};
Node* find(std::vector<Node>& Nodes, int ID)
{
for(auto i=Nodes.begin(); i!=Nodes.end(); ++i)
{
if (i->id() == ID)
{
return &*i;
}
}
}
main()
{
std::vector<Node> Nodes;
// fill Nodes with data
Grid Array;
Array.associate( find(Nodes,14325) );
Array.associate( find(Nodes,51384) );
Array.associate( find(Nodes,321684) );
// . . .
Nodes.push_back(Node(21616)); // this can invalidate my pointers in Array
}
If I was able to make my Nodes vairable be
const_lock<std::vector<Node>> Nodes;
then call
Nodes.set_const_lock(true);
after populating the data, I wouldn't need to worry about my pointers in Array getting messed up.

How to tell that there is no result of a function with return value?

I have a short question. Given a function which returns an object of a class as result, what should I return if there is no result (say because an index is out of range)? I could return a new "empty" object but how can I point out that there was no successful calculation?
I suppose there is a common approach.
The common approach in C++ is either to throw an exception or to use some wrapper like boost::optional.
An exception should be thrown if it is some kind of error, the boost::optional-approach is more appropriate if it is a valid use-case of your function to return an empty result. One example that comes to mind is SQL's NULL. boost::optional turned out quite handy in our codebase.
Going by the philosophy of the vector::at method throw out_of_range exception if possible.
If we are talking about erroneous situation, throwing an exception is proper solution.
#include<exception>
Object * GenerateObject(int i)
{
if (i < 0)
throw std::out_of_range("i");
return new Object(i);
}
int main(int argc, char * argv[])
{
try
{
Object * obj = GenerateObject(-1);
// Succeeded
return 0;
}
catch (std::exception & e)
{
// Failed, exiting with error value
return 1;
}
}
If an empty value is allowed, you can specify a specific value for this class, eg.
class Rectangle
{
private:
int left, top, width, height;
public:
Rectangle(l, t, w, h)
{
left = l;
top = t;
width = w;
height = h;
}
public static Rectangle empty;
}
Rectangle Rectangle::empty = Rectangle(0, 0, -1, -1);
// ...
Rectangle DoSth(int i)
{
// i < 0 is NOT considered an error here
if (i < 0)
return Rectangle::empty;
// Further processing
}
It depends on what the semantics of the operation is.
If an error occurred, you should definitely throw an exception:
#include <stdexcept> // Necessary for standard exceptions
X foo()
{
...
if (/* something goes wrong... */)
{
// There may be a more appropriate exception class. You could
// also derive your own exception class from std::exception...
throw std::logic_error("Whatever!");
}
...
}
...
try
{
X x = foo();
// Work with x...
}
catch (std::logic_error const& e) // Catch what is appropriate...
{
std::cout << e.what();
}
If returning a no-value does not denote an error condition, you could use Boost.Optional. Alternatively, provided you can create an "empty" object of type X, you could think of returning a pair whose second member is a bool flag that tells whether the first member is a valid object or not, as below:
std::pair<X, bool> foo();
...
bool valid;
X x;
std::tie(x, valid) = foo();
if (valid)
{
// Use x...
}
You can throw an exception when values don't match expected results.
A tutorial can be found at http://www.cplusplus.com/doc/tutorial/exceptions
An exception works with a try and catch principle.
A program "tries" to execute code.
If something unexpected happens the executed code "throws" an object, variable or whatever and this will be caught.
In the catch statement you can place code what should happen if the unexpected has happened.
Just follow the tutorial.
You can pair an enumeration with the object type being returned. If the returned enumeration is a certain value, the object is valid, otherwise the object is in an invalid state.
// This is a working C++11 example.
#include <utility>
#include <memory>
enum result
{
ok,
out_of_range,
some_other_error
};
class object
{
public:
object() {}
};
typedef std::shared_ptr< object > object_ptr;
typedef std::pair< result, object_ptr > return_type;
return_type some_function( int index )
{
if ( index > 5 )
{
return return_type{ result::out_of_range, nullptr };
}
return return_type{ result::ok, object_ptr{ new object() } };
}
int main()
{
return_type res = some_function( 10 );
if ( res.first == result::ok )
{
// Do something with res.second
}
else
{
// Handle the error
}
}
I would probably just throw an exception instead.

Template to perform automatic memory overwrite on destruction

It is a common pattern to use templates to enforce the compiler to initialize primitive / POD types values (https://stackoverflow.com/a/11493744/16673 or http://www.codeproject.com/Articles/825/Using-templates-for-initialization).
Does a similar pattern exist that could be used to erase the value once it goes out of scope for security reasons, to make sure the value is not left on the stack once the variable is destructed? I am afraid a naive analogous implementation might not work, as the compiler is free to ignore any assignments to a value which is going out of scope, as the value can be trivially proven not to be used any more. Is there some consistent and reasonably portable solution e.g. using volatile?
There's a function in the Windows API called SecureZeroMemory. You could look at it's implementation.
However, generally speaking, the compiler is forced to honour volatile writes. If you made the variable volatile, it should not be able to remove writes.
You could use some c++11 features to make this more portable, but this may suffice as a starting point:
Class
template<typename T>
class t_secure_destruct {
static const size_t Size = sizeof(T);
static const size_t Align = alignof(T);
public:
t_secure_destruct() : d_memory() {
new(this->d_memory)T;
}
~t_secure_destruct() {
reinterpret_cast<T*>(this->d_memory)->~T();
this->scribble();
}
// #todo implement or delete op-assign and remaining constructors
public:
T& get() {
return *reinterpret_cast<T*>(this->d_memory);
}
const T& get() const {
return *reinterpret_cast<const T*>(this->d_memory);
}
private:
void scribble() {
for (size_t idx(0); idx < Size; ++idx) {
this->d_memory[idx] = random();
}
}
private:
__attribute__((aligned(Align))) char d_memory[Size];
};
Demo
#include <iostream>
class t_test {
public:
t_test() : a(-1) {
std::cout << "construct\n";
}
~t_test() {
std::cout << "destruct\n";
}
public:
void print() const {
std::cout << "a = " << a << "\n";
}
public:
int a;
};
int main(int argc, const char* argv[]) {
t_secure_destruct<t_test>test;
test.get().print();
test.get().a = 100;
test.get().print();
return 0;
}
Of course, you could also back that allocation with a heap allocation, if you favor. If you need to outsmart an optimizer, you may need to put the scribbler out of its reach.