Skip to content

in-class initialization for this pointer doesn't work on CRTP. #19592

@llvmbot

Description

@llvmbot
Bugzilla Link 19218
Version 3.4
OS Linux
Reporter LLVM Bugzilla Contributor
CC @DougGregor

Extended Description

template <class Derived>
class base
{
    Derived* _ptr{static_cast<Derived*>(this)};
};

class test
    : public base<test>
{
};

The above code doesn't compile.

However if one is to write a custom constructor as follow it would work:

template <class Derived>
class base
{
public:
    base() : _ptr{static_cast<Derived>(this)} {}
private:
    Derived* _ptr{};
};

Not sure if true, but I vaguely remember that in-class initialization should have the same side-effect as constructor initialization lists.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++11clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions