Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:09:23+00:00 2026-06-11T12:09:23+00:00

I would like to create a template function that returns either int or std::vector<int>

  • 0

I would like to create a template function that returns either int or std::vector<int> depending on a template parameter. For example:

struct ReturnInt {};
struct ReturnVec {};

[...]

int num = func<ReturnInt>();
std::vector<int> nums = func<ReturnVec>();

I’ve been attempting naively to implement this based on my very limited experience with TMP. I feel like it should involve something along the lines of explicit template specializations, std::enable_if, std::conditional, and/or SFINAE. But none of my attempts to code this will compile, let alone run in a simple test.

How would this return-type switching be implemented?

Edit: As noted in the comments, this is a simplification of my actual problem. If it helps, I have a class that accepts a template parameter. Depending on the parameter, I would like its get() method to return either a single object/value, or a standard container of objects/values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:09:25+00:00Added an answer on June 11, 2026 at 12:09 pm

    Based on your edit, you just want a bog-standard template specialization

    struct ReturnsInt{};
    struct ReturnsVec{};
    
    template<typename T>
    class Foo {};
    
    template<>
    class Foo<ReturnsInt> {
    public:
        int get() { return 3; }
    };
    
    template<>
    class Foo<ReturnsVec> {
    public:
        std::vector<int> get() {
            return {3};
        }
    };
    

    If you only want to modify one member function based on the template, but reuse the rest of the class definition, you can use a helper class

    struct ReturnsInt{};
    struct ReturnsVec{};
    
    class helper_Foo {
    public:
        string bar() {
            return "this is a shared method";
        }
    };
    
    template<typename T>
    class Foo : public helper_Foo {};
    
    template<>
    class Foo<ReturnsInt> : public helper_Foo {
    public:
        int get() { return 3; }
    };
    
    template<>
    class Foo<ReturnsVec> : public helper_Foo {
    public:
        std::vector<int> get() {
            return {3};
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a function that returns something like an array of
In order to not repeat myself, I would like to create a function that
I would like to create a template for new stored procedures for our development
I would like to create a handlebars template and use a local helper for
I would like to create the ASP.NET User database template on a database of
I would like to create a c++ type that mimic the build-in type exactly.
I would like to create a class that runs something (a runnable) at regular
I would like to create a base class that will be inherited by other
I want to write a function that creates a Vector (template), stores a couple
i would like create a array of structure which have a dynamic array :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.