New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support const alias_tensor_instance #204
Comments
Not in any clean way since alias_tensor_const_instance needs to inherit from tensor. So you will be able to pass it to functions that take a You will also have a problem where an alias_tensor_const_instance is unusable unless it is itself declared const because the non-const members will be selected automatically when they are called, leading to exceptions being thrown. I'm open to ideas to make it workable but it's not super clear how to do it without a bunch of problems. |
What if: return simply a |
C++11 defines const constructors? I don't think that exists. Can you give
a code example?
|
Not const constructors, const rvalue reference constructors. But on a second thought, this is a recipe for a disaster... class PointerHolder
{
public:
PointerHolder(const PointerHolder&& other)
{
ptr = other.ptr;
/* Can't be done, unless ptr is mutable: */
//other.ptr = nullptr;
}
private:
PointerHolder(int *ptr):
ptr(ptr)
{}
friend const PointerHolder pointer_holder_creator(int *ptr);
int *ptr;
};
const PointerHolder pointer_holder_creator(int *ptr)
{
const PointerHolder ret(ptr);
return ret;
}
int main()
{
int *a = new int[42];
{
const PointerHolder h(pointer_holder_creator(a));
h; // ...
}
delete[] a;
} I don't know how to make it work on classes with non-trivial destructors without using mutable, because you wouldn't be able to change the state of the rvalue referenced object to a valid destructible state. One of possibly many problems is that one would be allowed to create a non-const version of a const object, like: PointerHolder const_removed_h(std::move(h)); |
Right, the central problem is the assignment to a non-const version of the
object. That's the reason I didn't make a const version of alias tensor.
|
Hope you don't mind, I've asked on StackOverflow: |
I deleted my previous comment because I think I didn't really understood the problem, and the solution proposed was wrong. The suggestion given by Yakk on Stack Overflow seems to work like this: class alias_tensor_const_holder
{
public:
const tensor& get() const {
return t;
}
private:
const alias_tensor_instance t;
}
/* ... */
alias_tensor_const_holder operator() (
const tensor& t,
size_t offset
); The copy to non-const is solved by hiding the object privately, and never exposing it directly, but only through a |
Very good ideas on stack overflow 👍 That's definitely a good solution. Adding an implicit conversion in addition to a get() makes it a really nice solution. I'll add this to dlib. Or you can make a pull request with the changes if you like :) |
Although I am not sure how to build a |
You can do the conversion like this: |
Please do it, or I'm afraid I'll waste effort trying to match the code style only to be rejected in the end. I was trying to do without using |
I just pushed the code for this. So you should be able to use const aliasing tensors now. Thanks for suggesting the idea :) |
It would be nice if
alias_tensor
class had support forconst tensor&
, something like:Is it feasible?
The text was updated successfully, but these errors were encountered: