Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm having the following issue with constexpr arrays and CRTP. While the first line works the second yields an error that Der::arr does not exist. Given that the first line should execute in a constexpr context, I'm not sure what the issue is with the second.

template <typename Der>
struct Base
{   
    static constexpr std::size_t val = Der::arr.size(); // This works
    static constexpr std::array<int, Der::arr.size()> r = {}; // yields error
};

struct Derived : Base<Derived>
{
    static constexpr std::array<int, 10> arr = {};
};

int main(int argc, char **argv){
    Derived d;  
    return d.val;
}

Error:

<source>:12:43: error: no member named 'arr' in 'Derived'
    static constexpr std::array<int, Der::arr.size()> r = {}; // yields error
                                     ~~~~~^
<source>:15:18: note: in instantiation of template class 'Base<Derived>' requested here
struct Derived : Base<Derived>
                 ^
1 error generated.
Compiler returned: 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
199 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...