Type conversion between Node, Const_node, and their views

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












Each node is a location within a layer. A Location may be a pointer or an index. A Layer is a container.



There are 4 types of node per Layer and Location types:



  • Node: Can change content of layer. Not a view of another node.

  • Const_node: Cannot change content of layer. Not a view of another node.

  • Node_view: Can change content of layer. A view of a Node.

  • Const_node_view: Cannot change content of layer. A view of a Const_node.

The code implements the types, their conversions, and traits.



Thanks for reviewing this.



Code:



#include <type_traits>
#include <vector>
#include <iostream>

// Node
template<typename Layer, typename Location=typename Layer::size_type>
class Node
public:
Node(Layer& lay, Location loc)
: layer(layer), location(loc)

auto content() return layer[location];

template<typename Other_layer, typename Other_location,
typename=std::enable_if_t<
std::is_constructible<Layer, Other_layer>::value
&& std::is_constructible<Location, Other_location>::value
>
>
operator Node<Other_layer, Other_location>()
return Node<Other_layer, Other_location>(
layer, location
);
;

Node& operator=(const Node& n) location = n.location;

private:
Layer& layer;
Location location;
;

template<typename L, typename Loc=typename L::size_type>
using Node_view=Node<L, Loc&>;
template<typename L, typename Loc=typename L::size_type>
using Const_node=Node<const L, Loc>;
template<typename L, typename Loc=typename L::size_type>
using Const_node_view=Node<const L, Loc&>;

// Traits
template<template<typename...> class, typename...>
struct can_instantiate_to : public std::false_type
;

template<template<typename...> class U, typename... T>
struct can_instantiate_to<U, U<T...>> : public std::true_type
;

template<typename T>
struct is_node : std::integral_constant<
bool, can_instantiate_to<Node, T>::value>
;


template<typename T, typename=std::enable_if_t<is_node<T>::value>>
void foo(T node)

int main()
using V = std::vector<int>;
V v(10u);
// Node n1(v, 0); // Error as Location type is deduced to be int.
Node<V> n1(v, 0u);
Node_view<V> n2(n1);
Const_node<V> n3(n1);
Const_node_view<V> n4(n1);

foo(n1);
foo(n2);
foo(n3);
foo(n4);

return 0;







share|improve this question



















  • is_constructible works but is_convertible doesn't.
    – R zu
    May 14 at 2:49










  • Maybe that Location should just be a bool to show if the Node is a view or not. The Layer can provide the size_type. This will need to more things from <type_traits>, but it can avoid incorrect deduction of the Location type. The user also doesn't have to remember the correct Location type all of the time.
    – R zu
    May 14 at 3:35











  • On the other hand, this means I have to make two node classes. One for pointer, another one for array... Pointer/array nodes are not convertible to each other. But I want the same algorithm to work on both kinds of nodes.
    – R zu
    May 14 at 3:41











  • No need for two classes for array/pointer nodes. Just make the underlying container always return content of node by indexing operation. Maybe there is no need to check the Location type. Because int, int& are convertible in both direction anyway.
    – R zu
    May 14 at 15:36

















up vote
1
down vote

favorite












Each node is a location within a layer. A Location may be a pointer or an index. A Layer is a container.



There are 4 types of node per Layer and Location types:



  • Node: Can change content of layer. Not a view of another node.

  • Const_node: Cannot change content of layer. Not a view of another node.

  • Node_view: Can change content of layer. A view of a Node.

  • Const_node_view: Cannot change content of layer. A view of a Const_node.

The code implements the types, their conversions, and traits.



Thanks for reviewing this.



Code:



#include <type_traits>
#include <vector>
#include <iostream>

// Node
template<typename Layer, typename Location=typename Layer::size_type>
class Node
public:
Node(Layer& lay, Location loc)
: layer(layer), location(loc)

auto content() return layer[location];

template<typename Other_layer, typename Other_location,
typename=std::enable_if_t<
std::is_constructible<Layer, Other_layer>::value
&& std::is_constructible<Location, Other_location>::value
>
>
operator Node<Other_layer, Other_location>()
return Node<Other_layer, Other_location>(
layer, location
);
;

Node& operator=(const Node& n) location = n.location;

private:
Layer& layer;
Location location;
;

template<typename L, typename Loc=typename L::size_type>
using Node_view=Node<L, Loc&>;
template<typename L, typename Loc=typename L::size_type>
using Const_node=Node<const L, Loc>;
template<typename L, typename Loc=typename L::size_type>
using Const_node_view=Node<const L, Loc&>;

// Traits
template<template<typename...> class, typename...>
struct can_instantiate_to : public std::false_type
;

template<template<typename...> class U, typename... T>
struct can_instantiate_to<U, U<T...>> : public std::true_type
;

template<typename T>
struct is_node : std::integral_constant<
bool, can_instantiate_to<Node, T>::value>
;


template<typename T, typename=std::enable_if_t<is_node<T>::value>>
void foo(T node)

int main()
using V = std::vector<int>;
V v(10u);
// Node n1(v, 0); // Error as Location type is deduced to be int.
Node<V> n1(v, 0u);
Node_view<V> n2(n1);
Const_node<V> n3(n1);
Const_node_view<V> n4(n1);

foo(n1);
foo(n2);
foo(n3);
foo(n4);

return 0;







share|improve this question



















  • is_constructible works but is_convertible doesn't.
    – R zu
    May 14 at 2:49










  • Maybe that Location should just be a bool to show if the Node is a view or not. The Layer can provide the size_type. This will need to more things from <type_traits>, but it can avoid incorrect deduction of the Location type. The user also doesn't have to remember the correct Location type all of the time.
    – R zu
    May 14 at 3:35











  • On the other hand, this means I have to make two node classes. One for pointer, another one for array... Pointer/array nodes are not convertible to each other. But I want the same algorithm to work on both kinds of nodes.
    – R zu
    May 14 at 3:41











  • No need for two classes for array/pointer nodes. Just make the underlying container always return content of node by indexing operation. Maybe there is no need to check the Location type. Because int, int& are convertible in both direction anyway.
    – R zu
    May 14 at 15:36













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Each node is a location within a layer. A Location may be a pointer or an index. A Layer is a container.



There are 4 types of node per Layer and Location types:



  • Node: Can change content of layer. Not a view of another node.

  • Const_node: Cannot change content of layer. Not a view of another node.

  • Node_view: Can change content of layer. A view of a Node.

  • Const_node_view: Cannot change content of layer. A view of a Const_node.

The code implements the types, their conversions, and traits.



Thanks for reviewing this.



Code:



#include <type_traits>
#include <vector>
#include <iostream>

// Node
template<typename Layer, typename Location=typename Layer::size_type>
class Node
public:
Node(Layer& lay, Location loc)
: layer(layer), location(loc)

auto content() return layer[location];

template<typename Other_layer, typename Other_location,
typename=std::enable_if_t<
std::is_constructible<Layer, Other_layer>::value
&& std::is_constructible<Location, Other_location>::value
>
>
operator Node<Other_layer, Other_location>()
return Node<Other_layer, Other_location>(
layer, location
);
;

Node& operator=(const Node& n) location = n.location;

private:
Layer& layer;
Location location;
;

template<typename L, typename Loc=typename L::size_type>
using Node_view=Node<L, Loc&>;
template<typename L, typename Loc=typename L::size_type>
using Const_node=Node<const L, Loc>;
template<typename L, typename Loc=typename L::size_type>
using Const_node_view=Node<const L, Loc&>;

// Traits
template<template<typename...> class, typename...>
struct can_instantiate_to : public std::false_type
;

template<template<typename...> class U, typename... T>
struct can_instantiate_to<U, U<T...>> : public std::true_type
;

template<typename T>
struct is_node : std::integral_constant<
bool, can_instantiate_to<Node, T>::value>
;


template<typename T, typename=std::enable_if_t<is_node<T>::value>>
void foo(T node)

int main()
using V = std::vector<int>;
V v(10u);
// Node n1(v, 0); // Error as Location type is deduced to be int.
Node<V> n1(v, 0u);
Node_view<V> n2(n1);
Const_node<V> n3(n1);
Const_node_view<V> n4(n1);

foo(n1);
foo(n2);
foo(n3);
foo(n4);

return 0;







share|improve this question











Each node is a location within a layer. A Location may be a pointer or an index. A Layer is a container.



There are 4 types of node per Layer and Location types:



  • Node: Can change content of layer. Not a view of another node.

  • Const_node: Cannot change content of layer. Not a view of another node.

  • Node_view: Can change content of layer. A view of a Node.

  • Const_node_view: Cannot change content of layer. A view of a Const_node.

The code implements the types, their conversions, and traits.



Thanks for reviewing this.



Code:



#include <type_traits>
#include <vector>
#include <iostream>

// Node
template<typename Layer, typename Location=typename Layer::size_type>
class Node
public:
Node(Layer& lay, Location loc)
: layer(layer), location(loc)

auto content() return layer[location];

template<typename Other_layer, typename Other_location,
typename=std::enable_if_t<
std::is_constructible<Layer, Other_layer>::value
&& std::is_constructible<Location, Other_location>::value
>
>
operator Node<Other_layer, Other_location>()
return Node<Other_layer, Other_location>(
layer, location
);
;

Node& operator=(const Node& n) location = n.location;

private:
Layer& layer;
Location location;
;

template<typename L, typename Loc=typename L::size_type>
using Node_view=Node<L, Loc&>;
template<typename L, typename Loc=typename L::size_type>
using Const_node=Node<const L, Loc>;
template<typename L, typename Loc=typename L::size_type>
using Const_node_view=Node<const L, Loc&>;

// Traits
template<template<typename...> class, typename...>
struct can_instantiate_to : public std::false_type
;

template<template<typename...> class U, typename... T>
struct can_instantiate_to<U, U<T...>> : public std::true_type
;

template<typename T>
struct is_node : std::integral_constant<
bool, can_instantiate_to<Node, T>::value>
;


template<typename T, typename=std::enable_if_t<is_node<T>::value>>
void foo(T node)

int main()
using V = std::vector<int>;
V v(10u);
// Node n1(v, 0); // Error as Location type is deduced to be int.
Node<V> n1(v, 0u);
Node_view<V> n2(n1);
Const_node<V> n3(n1);
Const_node_view<V> n4(n1);

foo(n1);
foo(n2);
foo(n3);
foo(n4);

return 0;









share|improve this question










share|improve this question




share|improve this question









asked May 14 at 2:44









R zu

2026




2026











  • is_constructible works but is_convertible doesn't.
    – R zu
    May 14 at 2:49










  • Maybe that Location should just be a bool to show if the Node is a view or not. The Layer can provide the size_type. This will need to more things from <type_traits>, but it can avoid incorrect deduction of the Location type. The user also doesn't have to remember the correct Location type all of the time.
    – R zu
    May 14 at 3:35











  • On the other hand, this means I have to make two node classes. One for pointer, another one for array... Pointer/array nodes are not convertible to each other. But I want the same algorithm to work on both kinds of nodes.
    – R zu
    May 14 at 3:41











  • No need for two classes for array/pointer nodes. Just make the underlying container always return content of node by indexing operation. Maybe there is no need to check the Location type. Because int, int& are convertible in both direction anyway.
    – R zu
    May 14 at 15:36

















  • is_constructible works but is_convertible doesn't.
    – R zu
    May 14 at 2:49










  • Maybe that Location should just be a bool to show if the Node is a view or not. The Layer can provide the size_type. This will need to more things from <type_traits>, but it can avoid incorrect deduction of the Location type. The user also doesn't have to remember the correct Location type all of the time.
    – R zu
    May 14 at 3:35











  • On the other hand, this means I have to make two node classes. One for pointer, another one for array... Pointer/array nodes are not convertible to each other. But I want the same algorithm to work on both kinds of nodes.
    – R zu
    May 14 at 3:41











  • No need for two classes for array/pointer nodes. Just make the underlying container always return content of node by indexing operation. Maybe there is no need to check the Location type. Because int, int& are convertible in both direction anyway.
    – R zu
    May 14 at 15:36
















is_constructible works but is_convertible doesn't.
– R zu
May 14 at 2:49




is_constructible works but is_convertible doesn't.
– R zu
May 14 at 2:49












Maybe that Location should just be a bool to show if the Node is a view or not. The Layer can provide the size_type. This will need to more things from <type_traits>, but it can avoid incorrect deduction of the Location type. The user also doesn't have to remember the correct Location type all of the time.
– R zu
May 14 at 3:35





Maybe that Location should just be a bool to show if the Node is a view or not. The Layer can provide the size_type. This will need to more things from <type_traits>, but it can avoid incorrect deduction of the Location type. The user also doesn't have to remember the correct Location type all of the time.
– R zu
May 14 at 3:35













On the other hand, this means I have to make two node classes. One for pointer, another one for array... Pointer/array nodes are not convertible to each other. But I want the same algorithm to work on both kinds of nodes.
– R zu
May 14 at 3:41





On the other hand, this means I have to make two node classes. One for pointer, another one for array... Pointer/array nodes are not convertible to each other. But I want the same algorithm to work on both kinds of nodes.
– R zu
May 14 at 3:41













No need for two classes for array/pointer nodes. Just make the underlying container always return content of node by indexing operation. Maybe there is no need to check the Location type. Because int, int& are convertible in both direction anyway.
– R zu
May 14 at 15:36





No need for two classes for array/pointer nodes. Just make the underlying container always return content of node by indexing operation. Maybe there is no need to check the Location type. Because int, int& are convertible in both direction anyway.
– R zu
May 14 at 15:36
















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

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: "196"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194337%2ftype-conversion-between-node-const-node-and-their-views%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194337%2ftype-conversion-between-node-const-node-and-their-views%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?