[value_initialized] Non-default constructors
I have a class template, templated on type T, which I need to hold as a data value in the class. I use value_initialized for this type T effectively, and the value gets value initialized correctly when the default constructor of my class is used. If, however, I add a second constructor to my class which takes a T, for initializing my 'value_initialized<T> data' value, things do not work as well. Obviously I can still value initialize my data value, as in my default constructor, and then use 'boost::get(data) = arg;' to set the value of my value initialized variable. But this will not work if the type T is a top-level const, whereas I do want to initialize the data value in this case to the T arg passed into my second constructor. Is there a good solution to this which I have overlooked ?
Edward Diener wrote:
I have a class template, templated on type T, which I need to hold as a data value in the class. I use value_initialized for this type T effectively, and the value gets value initialized correctly when the default constructor of my class is used.
If, however, I add a second constructor to my class which takes a T, for initializing my 'value_initialized<T> data' value, things do not work as well. Obviously I can still value initialize my data value, as in my default constructor, and then use 'boost::get(data) = arg;' to set the value of my value initialized variable. But this will not work if the type T is a top-level const, whereas I do want to initialize the data value in this case to the T arg passed into my second constructor.
Is there a good solution to this which I have overlooked ?
Never mind, the question was due to my unclear thinking about the problem.
Edward Diener wrote:
I have a class template, templated on type T, which I need to hold as a data value in the class. I use value_initialized for this type T effectively, and the value gets value initialized correctly when the default constructor of my class is used.
If, however, I add a second constructor to my class which takes a T, for initializing my 'value_initialized<T> data' value, things do not work as well. Obviously I can still value initialize my data value, as in my default constructor, and then use 'boost::get(data) = arg;' to set the value of my value initialized variable. But this will not work if the type T is a top-level const, whereas I do want to initialize the data value in this case to the T arg passed into my second constructor.
Never mind, the question was due to my unclear thinking about the problem.
So... how did you solve the problem? I'm curious because I was also thinking of proposing to change value_initialized somewhat... (I would like to get rid of its conversion operator!) Kind regards, -- Niels Dekker http://www.xs4all.nl/~nd/dekkerware Scientific programmer at LKEB, Leiden University Medical Center
Niels Dekker - mail address until 2008-12-31 wrote:
Edward Diener wrote:
I have a class template, templated on type T, which I need to hold as a data value in the class. I use value_initialized for this type T effectively, and the value gets value initialized correctly when the default constructor of my class is used.
If, however, I add a second constructor to my class which takes a T, for initializing my 'value_initialized<T> data' value, things do not work as well. Obviously I can still value initialize my data value, as in my default constructor, and then use 'boost::get(data) = arg;' to set the value of my value initialized variable. But this will not work if the type T is a top-level const, whereas I do want to initialize the data value in this case to the T arg passed into my second constructor.
Never mind, the question was due to my unclear thinking about the problem.
So... how did you solve the problem?
I solved it in my template by having the data value for T be of type T when T is top-level const, otherwise it is boost::value_initialized<T> when T is not a top-level const. The mpl::if_ metafunction and boost::is_const were used to generate the correct type, ala mpl::if_<boost::is_const<T>,T,boost::value_initialized<T> >::type data;
Edward Diener wrote:
I solved it in my template by having the data value for T be of type T when T is top-level const, otherwise it is boost::value_initialized<T> when T is not a top-level const. The mpl::if_ metafunction and boost::is_const were used to generate the correct type, ala
mpl::if_<boost::is_const<T>,T,boost::value_initialized<T> >::type data;
Okay, so your data is either a T, or a value_initialized<T>. I was wondering, do you use the fact that value_initialized<T> implicitly converts to reference-to-T? If your data is value_initialized<T>, you do boost::get(data) = arg. Would it be helpful to you to have an extra (explicit) boost::value_initialized<T> constructor, having const-reference to T as argument? Kind regards, Niels
Niels Dekker - mail address until 2008-12-31 wrote:
Edward Diener wrote:
I solved it in my template by having the data value for T be of type T when T is top-level const, otherwise it is boost::value_initialized<T> when T is not a top-level const. The mpl::if_ metafunction and boost::is_const were used to generate the correct type, ala
mpl::if_<boost::is_const<T>,T,boost::value_initialized<T> >::type data;
Okay, so your data is either a T, or a value_initialized<T>. I was wondering, do you use the fact that value_initialized<T> implicitly converts to reference-to-T?
If your data is value_initialized<T>, you do boost::get(data) = arg. Would it be helpful to you to have an extra (explicit) boost::value_initialized<T> constructor, having const-reference to T as argument?
I have moved away from solving my problem based on const. This is because in C++ there is the notion of 'read-only' ( const ) and 'read-write' ( no const ) whereas what I am designing also needs the notion of 'write-only'. Therefore I have a second template parameter, defaulting to 'read-write' to control that and I can specialize on that parameter. I may revisit the const issue in my design again, but for now I am giving a compile time error when my T has top level const.
participants (2)
-
Edward Diener
-
Niels Dekker - mail address until 2008-12-31