Home Different code path according to different type in a template method
Reply: 0

Different code path according to different type in a template method

user1004
1#
user1004 Published in April 27, 2018, 6:52 am

Let's say I have a template class like this:

template<typename TRequest, typename TResponse = void>
class handler
{
private:
    TResponse process_core(const TRequest& request);
public:
    TResponse process(const TRequest& request)
    {
        //log the request        
        TResponse response = process_core(request);  //return process_core(request) works;
        //log the response, for void it's fine to log nothing
        return response;
    }
};

Somewhere else in the project process_core is implemented for different TRequest/TResponse types. For example:

template<> void handler<Foo>::process_core(const Foo& foo) { }
template<> Baz handler<Bar, Baz>::process_core(const Bar& bar) { }

Obviously return response breaks for void types. What's the correct way to do this? Or my design is not the C++ way? I'm new to C++.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324806 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO