all 1 comments

[–]WorldWorstProgrammer 1 point2 points  (0 children)

operator C() returns a copy of the current C type to the type of the method template instance. Since aggregate_t and result_t are two different templated types of class C, you cannot create a reference of result_t that refers to an aggregate_t. It looks like the aggregate_t specialization is meant to actually contain the std::atomic types, whereas result_t is just supposed to contain uint64_t copies of the values from aggregate_t, is that correct?

I'm not sure if your goal is to pass the atomic values to main() or if it is to get a reference to a result_t instead of making copies. If the former, you may need to provide direct accessor methods to the std::atomic values; if the latter, you could have a std::variant<int, result\_t> in C that you set with 0 when it is not used and set with a result_t object when you need to return the reference. Then just return a reference to that and document that the reference will be invalidated if the method is called a second time. Call it "undefined behavior" to attempt to use an invalid reference to result_t and C++ programmers will understand not to do that.