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

  • Home
  • SEARCH
  • 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 8056499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:43:23+00:00 2026-06-05T08:43:23+00:00

If I template a function in C++ on the return type: template<class T> T

  • 0

If I template a function in C++ on the return type:

template<class T> T foo(int x) { };

I can use explicit template specialization to create a double instance:

template<> double foo<double>(int x) { };

I tried to use forward declaration to create an instance of an undefined struct:

struct bar;
template<> bar foo<bar>(int x) { }

This fails with error: return type ‘struct bar’ is incomplete. I thought this would be allowed, even after reading the accepted answer on When to use forward declaration? since, until I create an instance of foo<bar>, the explicit templated code is never created. Clearly this is not the case (why not?).

Is it possible to have code for functions whose type might not exist at compile time, and only compile the code if the particular explicit specialization is instanced? The motivation for this is an “overloaded” struct reader, where the overload operations are procedurally generated before the compile (via a script).

  • 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-05T08:43:25+00:00Added an answer on June 5, 2026 at 8:43 am

    When you define an specialization of a function you are defining the function, not a function template. The rules at this point are exactly the same as for any regular function and because you are using the type in a way that needs a complete type, the lack of a definition for bar makes your program ill-formed.

    (The lack of a return statement also makes the program ill-formed…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A function template: template<class T> T max(T a, T b){return (a > b)? a:
When I define this function, template<class A> set<A> test(const set<A>& input) { return input;
I'm trying to create an is_foo function, that I can then use with enable_if
I have this template function: template <class P> double Determinant(const P & a, const
If you have this generic function: template<class type, class ret, class atype1, class atype2,
The following function is defined for all arithmetic types: template <class T> typename enable_if_c<boost::is_arithmetic<T>::value,
I want to specialize a class template with the following function: template <typename T>
I've been looking for a class in MPL that will create a function object
I'm trying to use decltype in the late specified return of a member function
One of my classes declares a templated function: template<class A, class B> A do_something(const

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.