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 6863097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:44:27+00:00 2026-05-27T02:44:27+00:00

Is there a better way to do the following? #include <iostream> template <typename T>

  • 0

Is there a better way to do the following?

#include <iostream>

template <typename T>
T Bar();

template <>
int Bar<int>() { return 3; }

// Potentially other specialisations

int main()
{
    std::cout << Bar<int>() << std::endl; // This should work
    std::cout << Bar<float>() << std::endl; // This should fail
}

The problem with this solution is that it fails at (understandably) link time with “undefined reference to float Bar<float>()” or the like. This can be confusing for other developers as they may suspect an implementation file is not being linked.

I do know another potential solution:

template <typename T>
T Bar() { BOOST_STATIC_ASSERT(sizeof(T) == 0); }

This causes a compiler error when Bar<float>() is requested, exactly what I want. However, I’m concerned that technically a compiler may reject this just as gcc rejects BOOST_STATIC_ASSERT(false) because it knows that it will fail regardless of the template parameter, since sizeof(T) can never be zero.

In summary, I want to know whether:

  1. There is another way to do this.
  2. I’m mistaken and BOOST_STATIC_ASSERT(sizeof(T)) actually can’t fail without instantiation.
  3. The only way is to let this be a linker error as above.
  • 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-05-27T02:44:28+00:00Added an answer on May 27, 2026 at 2:44 am

    BOOST_STATIC_ASSERT(sizeof(T) == 0); isn’t allowed to fail until the template is instantiated, so I would just do that one. You are correct that BOOST_STATIC_ASSERT(false); triggers each time.


    The reason for this has to do with two-phase name lookup. This is, essentially, the following: when a template is compiled, it’s compiled twice. The first time a compielr sees a template it compiles everything except the expressions dependent on template parameters, and the second compilation happens once the template parameter is known, compiling the instantiation fully.

    This is why BOOST_STATIC_ASSERT(false); will fail always: nothing here is dependent and the assert is processed immediately, as if the function weren’t a template at all. (Note that MSVC does not implement two-phase look-up, so this fails at instantiation, incorrectly.) Contrarily, because T is dependent (§14.6.2.1), BOOST_STATIC_ASSERT(sizeof(T) == 0); is dependent, and is not allowed to be checked until the template is instantiated. (Where upon it will always fail.)

    If a compiler tries to be thoughtful and fail it ahead of time, it would be non-conforming. You’re suppose to be able to rely on this stuff. That said, if fear gets the best of you it’s trivial to really make it wait:

    BOOST_STATIC_ASSERT(sizeof(typename T::please_use_specializations) == 0);
    

    This is both guaranteed to fail, and impossible for a compiler to correctly “smartly” fail ahead of time.

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

Sidebar

Related Questions

Is there a better way than the following brute foce implementation of a c#
Is there a better way of writing the following for loop? my_list = []
Is there a better way to write the following function? Having the '#' +
Is there a better way of writing the following method? Using LINQ maybe? Instead
I'm hoping there is a better way to the following. I'm creating a jigsaw-type
Is there a better/shorter way to write the whoAmI method in the following code?
Is there a better (shorter?) way than the following? let cpucount = System.UInt16.Parse( reader.GetInt32(3).ToString()
Is there a better way to code this? def __contains__(self, e): return e in
Is there better way to delete a parameter from a query string in a
Is there better way to enumerate all photos on the device than this one?

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.