Is aggregate initialization with user-defined fields possible in a struct with members derived from a template parameter including these fields?

Background

I’m aware the question is quite the mouthfull, so I’ll try to explain the situation to the best of my ability.

Aggregate initialization of templated classes has become quite easy to utilize with C++20 onwards due to implicit class template argument deduction. However, most implimentations taking advantage of this create explicitly named member variables.

I am currently working with a processor that has an otherwise unused register and am creating a tiny library to interact with it and the rest of the processor; ideally, users should have the ability to arbitrarily define the abstract contents of the register (e.g., a pointer, four uint8 indices, etc.) without having to utilize a variable name forced upon them by the library.

Ideally, the user would not have to create an object that inherits from a base register class and would be able to utilize aggregate initialization.

Problem

The following C++23 implementation that is functional, but non-ideal.

/* Library */


template <class UserInterpretation>
struct Register : public UserInterpretation {
    void MoveToPhysicalRegister(void) {
        u32& contents = *__builtin_bit_cast(u32*, this);
        asm(...); // Processor-specific assembly to move to desired register
    }
    void MoveFromPhysicalRegister(void) {
        asm(...); // Processor-specific assembly to move from desired register
    }
};

This implementation works fine when the default constructor is called before any modifications to the object via the user-defined interpretation. With implicit template argument deduction, constructing a RegisterInterpretation object in place works as well. However, attempting aggregate initialization of the register object using the user-defined variables fails.

/* User */


struct RegisterInterpretation {
    u32 InitMemAddr : 24;
    u8  InitMode;
};

// Works
void UserFunctionDefaultConstructorFollowedByModifications(void) {
    Register<RegisterInterpretation> userRegister;
    userRegister.InitMemAddr = 0xFB9000;
    userRegister.InitMode = 3;
    userRegister.MoveToPhysicalRegister();
    userRegister.InitMode = 2;
    userRegister.MoveToPhysicalRegister();
}

// Works
void UserFunctionConstructionInPlace(void) {
    Register userRegister = {RegisterInterpretation{.InitMemAddr = 0xFB9000, .InitMode = 3}};
    userRegister.MoveToPhysicalRegister();
    userRegister.InitMode = 2;
    userRegister.MoveToPhysicalRegister();
user
}

// Fails
void UserFunctionAttemptedAggInitError(void) {
    Register<RegisterInterpretation> userRegister = {.InitMemAddr = 0xFB9000, .InitMode = 0}; // ERROR
    userRegister.MoveToPhysicalRegister();
    userRegister.InitMode = 2;
    userRegister.MoveToPhysicalRegister();
}

Generated Error

In the failed instance, two errors of the same type are generated.

error: field designator 'InitMemAddr' does not refer to any field in type 'Register<RegisterInterpretation>'
error: field designator 'InitMode' does not refer to any field in type 'Register<RegisterInterpretation>'

Note that the same result is obtained with both Clang and GCC.

Questions

I have two questions regarding the observed behavior:

  1. The register object inherits from its template parameter, so why are the fields of the template parameter not recognized as fields for the register object?
  2. Is there anything in the C++ standard that explains these results?

New contributor

S-Adenosyl-L-Methionine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật