Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Who can solve this C++ problem? (Making std::format work with your own object types)

+7 votes
asked Jan 9 by Areeb Sherjil (1,920 points)

I want to be able to write the following:

std::format("My new box, Box({:.2},{:.2},{:.2})", box.getLength(), box.getWidth(), box.getHeight());

to

std::format("My new box, Box({:.2},{:.2},{:.2})", box.getLength(), box.getWidth(), box.getHeight());

This is what I've done so far:

template <>

class std::formatter<Box> :public std::formatter<double>

{

public:

    using std::formatter<double>::format;

    auto format(const Box& box, auto& context)

    {

        auto iter = format(box.getLength(), context);

        context.advance(iter);

        out = format(box.getWidth(), context);

        context.advance(iter);

        return format(box.getHeight(), context);

    

    }

}; The box class definition is inside another header file. As you can see this code above does not work.

2 Answers

0 votes
answered Jan 11 by xDELLx (9,400 points)
First , which compiler does this compile on? I tried the online compiler here , latest gcc on local didnt work.

Second , If it compiled for you ,maybe you can shrae the error !1
commented Jan 22 by Areeb Sherjil (1,920 points)
Can you show me an example of how I can use std::format to print out 'Box' class object? The code I showed was my start but I'm not sure how to do this. I use Microsoft C++ compiler.
–1 vote
answered Jan 18 by ruki_tryuki (140 points)

I don't know how fix that

commented Jan 19 by Peter Minarik (55,660 points)
Then don't comment as it is really not helping anyone. :)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...