Is there a way to have read-and-write views in Range-v3?
In Range-v3 one can easily create view of existing containers. For example
#include<range/v3/view/transform.hpp>
#include<cassert>
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::transform((auto x){return x*2;});
assert( range[1] == 4 );
// range[1] = 4; // error cannot write to an l-value
}
These views are naturally read-only. I wonder if there is any way to create read and write view within Range-v3?
I do appreciate that such thing is much more complicated than read-only, and not always possible, but still I wonder if there is a protocol in Range-v3 to use this directly or implement it.
For example, I have seen examples of derived classes from ranges::view_facade
that implement a member function read()
(I cannot find the example again, the documentation of Ranges v3 is really scattered around). What about a write(...)
member function?
I am looking for some hypothetical bi
directional transform
code like this:
#include<range/v3/view/transform.hpp>
#include<cassert>
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::bitransform((double x){return x*2;}, (double x){return x/2;}); // direct and inverse function for read and write
assert( range[1] == 4 );
range[1] = 8; // error cannot write to an l-value
assert( range[1] == 8 );
assert( v[1] == 4 );
}
c++11 proxy rvalue lvalue range-v3
add a comment |
In Range-v3 one can easily create view of existing containers. For example
#include<range/v3/view/transform.hpp>
#include<cassert>
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::transform((auto x){return x*2;});
assert( range[1] == 4 );
// range[1] = 4; // error cannot write to an l-value
}
These views are naturally read-only. I wonder if there is any way to create read and write view within Range-v3?
I do appreciate that such thing is much more complicated than read-only, and not always possible, but still I wonder if there is a protocol in Range-v3 to use this directly or implement it.
For example, I have seen examples of derived classes from ranges::view_facade
that implement a member function read()
(I cannot find the example again, the documentation of Ranges v3 is really scattered around). What about a write(...)
member function?
I am looking for some hypothetical bi
directional transform
code like this:
#include<range/v3/view/transform.hpp>
#include<cassert>
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::bitransform((double x){return x*2;}, (double x){return x/2;}); // direct and inverse function for read and write
assert( range[1] == 4 );
range[1] = 8; // error cannot write to an l-value
assert( range[1] == 8 );
assert( v[1] == 4 );
}
c++11 proxy rvalue lvalue range-v3
add a comment |
In Range-v3 one can easily create view of existing containers. For example
#include<range/v3/view/transform.hpp>
#include<cassert>
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::transform((auto x){return x*2;});
assert( range[1] == 4 );
// range[1] = 4; // error cannot write to an l-value
}
These views are naturally read-only. I wonder if there is any way to create read and write view within Range-v3?
I do appreciate that such thing is much more complicated than read-only, and not always possible, but still I wonder if there is a protocol in Range-v3 to use this directly or implement it.
For example, I have seen examples of derived classes from ranges::view_facade
that implement a member function read()
(I cannot find the example again, the documentation of Ranges v3 is really scattered around). What about a write(...)
member function?
I am looking for some hypothetical bi
directional transform
code like this:
#include<range/v3/view/transform.hpp>
#include<cassert>
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::bitransform((double x){return x*2;}, (double x){return x/2;}); // direct and inverse function for read and write
assert( range[1] == 4 );
range[1] = 8; // error cannot write to an l-value
assert( range[1] == 8 );
assert( v[1] == 4 );
}
c++11 proxy rvalue lvalue range-v3
In Range-v3 one can easily create view of existing containers. For example
#include<range/v3/view/transform.hpp>
#include<cassert>
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::transform((auto x){return x*2;});
assert( range[1] == 4 );
// range[1] = 4; // error cannot write to an l-value
}
These views are naturally read-only. I wonder if there is any way to create read and write view within Range-v3?
I do appreciate that such thing is much more complicated than read-only, and not always possible, but still I wonder if there is a protocol in Range-v3 to use this directly or implement it.
For example, I have seen examples of derived classes from ranges::view_facade
that implement a member function read()
(I cannot find the example again, the documentation of Ranges v3 is really scattered around). What about a write(...)
member function?
I am looking for some hypothetical bi
directional transform
code like this:
#include<range/v3/view/transform.hpp>
#include<cassert>
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::bitransform((double x){return x*2;}, (double x){return x/2;}); // direct and inverse function for read and write
assert( range[1] == 4 );
range[1] = 8; // error cannot write to an l-value
assert( range[1] == 8 );
assert( v[1] == 4 );
}
c++11 proxy rvalue lvalue range-v3
c++11 proxy rvalue lvalue range-v3
asked Jan 20 at 6:45
alfCalfC
5,08222960
5,08222960
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can make transform_view
have elements of proxy type:
#include<vector>
#include<range/v3/view/transform.hpp>
#include<cassert>
struct proxy {
double& x;
operator double() { return x*2; }
void operator=(double y) { x = y / 2; }
};
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::transform((auto& x){return proxy{x};});
assert( range[1] == 4 );
range[1] = 8;
assert( range[1] == 8 );
assert( v[1] == 4 );
}
Of course. Excellent. I am surprised thatvoid operator=(double y)
doesn't need to beconst
. Given that the proxy will a temporary in this context.
– alfC
Jan 21 at 8:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54274223%2fis-there-a-way-to-have-read-and-write-views-in-range-v3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can make transform_view
have elements of proxy type:
#include<vector>
#include<range/v3/view/transform.hpp>
#include<cassert>
struct proxy {
double& x;
operator double() { return x*2; }
void operator=(double y) { x = y / 2; }
};
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::transform((auto& x){return proxy{x};});
assert( range[1] == 4 );
range[1] = 8;
assert( range[1] == 8 );
assert( v[1] == 4 );
}
Of course. Excellent. I am surprised thatvoid operator=(double y)
doesn't need to beconst
. Given that the proxy will a temporary in this context.
– alfC
Jan 21 at 8:02
add a comment |
You can make transform_view
have elements of proxy type:
#include<vector>
#include<range/v3/view/transform.hpp>
#include<cassert>
struct proxy {
double& x;
operator double() { return x*2; }
void operator=(double y) { x = y / 2; }
};
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::transform((auto& x){return proxy{x};});
assert( range[1] == 4 );
range[1] = 8;
assert( range[1] == 8 );
assert( v[1] == 4 );
}
Of course. Excellent. I am surprised thatvoid operator=(double y)
doesn't need to beconst
. Given that the proxy will a temporary in this context.
– alfC
Jan 21 at 8:02
add a comment |
You can make transform_view
have elements of proxy type:
#include<vector>
#include<range/v3/view/transform.hpp>
#include<cassert>
struct proxy {
double& x;
operator double() { return x*2; }
void operator=(double y) { x = y / 2; }
};
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::transform((auto& x){return proxy{x};});
assert( range[1] == 4 );
range[1] = 8;
assert( range[1] == 8 );
assert( v[1] == 4 );
}
You can make transform_view
have elements of proxy type:
#include<vector>
#include<range/v3/view/transform.hpp>
#include<cassert>
struct proxy {
double& x;
operator double() { return x*2; }
void operator=(double y) { x = y / 2; }
};
int main(){
std::vector<double> v = {1,2,3};
auto range = v | ranges::v3::view::transform((auto& x){return proxy{x};});
assert( range[1] == 4 );
range[1] = 8;
assert( range[1] == 8 );
assert( v[1] == 4 );
}
answered Jan 20 at 7:24
cpplearnercpplearner
5,01521936
5,01521936
Of course. Excellent. I am surprised thatvoid operator=(double y)
doesn't need to beconst
. Given that the proxy will a temporary in this context.
– alfC
Jan 21 at 8:02
add a comment |
Of course. Excellent. I am surprised thatvoid operator=(double y)
doesn't need to beconst
. Given that the proxy will a temporary in this context.
– alfC
Jan 21 at 8:02
Of course. Excellent. I am surprised that
void operator=(double y)
doesn't need to be const
. Given that the proxy will a temporary in this context.– alfC
Jan 21 at 8:02
Of course. Excellent. I am surprised that
void operator=(double y)
doesn't need to be const
. Given that the proxy will a temporary in this context.– alfC
Jan 21 at 8:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54274223%2fis-there-a-way-to-have-read-and-write-views-in-range-v3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown