static constexpr on rack::simd::float_4?

hi,

i’d like to set static constexpr rack::simd::float_4 v10 = 10.0f; instead of const rack::simd::float_4 v10 = 10.0f;, but it seems i can’t (expression must have a constant value compiler’s hit).

why? how can i do it?

thanks

You might want to try it like this:

static const rack::simd::float_4 v10(10.0f);

does this work?

float_4 v10 = {10.f};

(put the initializer in curly braces)

Yes,

static const rack::simd::float_4 v10 = {10.0f};

works also. By “works” I mean it compiles, I’m assuming that it correctly assigns the value to all 4 floats in both cases, but I didn’t test it.

Just offhand, I think I remember once Andrew mentioning it might not be good to initialize with the curly braces for values other that 0.0; do you remember if this was true, or just my memory having some corrupt bits?

Yeah, I do remember that. I think maybe it’s for arrays, that if you just put one in there it won’t go to all of them. I’m new to the curly braces (learned it at work), but i think what it does is make a constexpr, and then pass it to the constructor of the object. Since float_4 has a special constructor that takes a single float and spreads all the values around it works.

I think float x[4] = { 10.f } will only initialize x[0].

got the same hit message:

image

if that’s true, this should be avoid so :open_mouth: :slight_smile:

Ah, yes, I think he said it’s best to initialize arrays with empty braces {}

you have to use const, not constexpr I think

now, I don’t know if you can do this:

std::vector x = { 4, 10.f};

@q8fuel marc means you need to use:

static const rack::simd::float_4 constexpr isn’t legal on the declaration part (I don’t think). If it’s not clear yet, I’m no expert on this stuff :wink:

In order to declare a type like this as constexpr, the constructor that you call must also be declared constexpr, which in this case it isn’t (and probably can’t be because of what it does internally).

Ah! thanks. Did not know that. But wait! What is a constexper constructor? Could you give an example?

Aren’t curly braces calling a matching constructor for stack-variables? I thought so but I’m no expert in C++…

Me neither! :slight_smile: I’ve never looked at the reference doc for the curly braces in initialization cases…

As I understand it, a constexpr constructor basically can be used to construct an instance of your object at compile time. As such, it can only instantiate member variables that can also be created as constexpr and only call functions that can be evaluated as constexpr.

For example, some of the constructors for std::vector are declared constexpr, so you are allowed to declare constexpr vectors (though I believe the vector element type must be constexpr constructible, and probably the allocator type too if you’re using a custom allocator).

Here’s an example from my own code:

/** A simple struct containing a rhythmic delay length */
struct DelayRhythm
{
    constexpr DelayRhythm (const std::string_view& name, const std::string_view& label, double tempoFactor) : name (name), label (label), tempoFactor (tempoFactor) {}

    inline String getLabel() const { return String (static_cast<std::string> (label)); }

    std::string_view name;
    std::string_view label;
    double tempoFactor;
};

static constexpr std::array<DelayRhythm, 19> rhythms {
    DelayRhythm ("Thirty-Second", "1/32", 0.125),
    ...
};

Basically, I wanted to be able to have a constexpr array of DelayRhythms (probably didn’t need them to be constexpr, but at least I learned something in the process), so I needed a constexpr constructor. Also note that I needed to use std::string_view since std::string is not constexpr constructible.


The reason I’m not sure simd::float_4 can be declared constexpr is that it needs to initialize an __m128, and I’m not sure if SSE intrinsics are constexpr-compatible. Besides, I’m not sure how useful vectorization would be for compile-time operations anyway.

1 Like

Thanks for the info, I didn’t know we could declare our own constructors constexpr (with the restrictions, but still neat).

yeah, totally new to me, too. reminds me - I was once in a grueling job interview - 28 people over two days. In one session there were four other people interviewing me, including danny hillis (founder of thinking machines). Some guy asks me “it says on your resume expert level c++. I’ve been teaching c++ at UCLA for 10 years, and I hardly consider myself an expert. So I’m really curious: what does an expert know”?

Oh my, tough question! How did you handle it?

pretty decently, if arrogantly. I said "I’m comparing myself the the average I run into in previous jobs. I know what all the keywords mean. That makes me an expert right there. Later when I got the job I learned a) it was all a waste of time, because my boss wanted to hire me and didn’t even ask the other 27 what they thought, and b) the c++ guy from UCLA was half time and no one liked him :wink:

2 Likes

Yeah, the world of C++ compile-time programming is a really deep rabbit hole. I honestly don’t know much beyond some constexpr rules, and simple templates stuff. Although, I think I remember reading somewhere that C++ compile-time programming is actually Turing-complete on its own.