constexpr w/c++ stdlib features??

Linda A. Walsh gcc@tlinx.org
Thu May 22 13:13:00 GMT 2014


Jonathan Wakely wrote:
> On 21/05/14 22:02 -0700, Linda A. Walsh wrote:
>>
>> I'm trying to convert various 'C'-isms in my code.
>>
>> I had something that "passed" the "constexpr-test",
>> namely, constexpr const char ** cpu_states_txt = {"USR", "Ni", "Sys"...};
>>
>> This compiled and worked just fine.  But I am trying to change it
>> to a vector or valarray:
>>
>> constexpr const vector<const char *> const cpu_states_txt ={"Usr, 
>> "...."};
> 
> Why do you need this to be constexpr?
> 
> If you really do nee it to be constexpr then you need to use a literal
> type that can be initialized at compile-time.
---
	I thought that since vector or valarray could have dimensions
specified at compile time, then they might support compile time constants
as well.  I didn't know the implementation couldn't use compile time
dimensions to allow it to be used as constexpr.

	Is it a requirement that std::vector use runtime allocation if
the object it is representing is fixed at compile time or is that an
the result of implementation?

	It *seemed*, that by tagging things as invariant (either at compile
or run time after some initialization), it could allow optimizations.  in this
case, it would be the run-time code to initialize "resources" that are
statically fixed before the program starts execution.

>>....  error: the type ‘const std::vector<const
>> char*>’ of constexpr variable ‘cpu_states_txt’ is not literal
> 
> Obviously since std::vector uses dynamic memory allocation it can't be
> a compile-time constant.
> 
> Again, why do you need it to be constexpr?
> What's wrong with just a const vector?
----
	Nothing, but since it doesn't change and is present at compile
time, how would I let the invariant data be done at compile time
and not run time?

	I'm trying to recode parts of this program in C++ specific
terms where it will decomplicate the code.  Since it seems doable in 'C',
I wanted to figure out how to have C++ do it.




More information about the Libstdc++ mailing list