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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:16:41+00:00 2026-06-06T13:16:41+00:00

I noticed that static assertions in class templates are not triggered when instantiations are

  • 0

I noticed that static assertions in class templates are not triggered when instantiations are typedef‘ed.

#include <type_traits>

template <typename T>
struct test_assert
{
    static_assert( std::is_same< T, int >::value, "should fail" );
};

typedef test_assert< float > t;

This code compiles without error. If I try to create an instance, then the assertion fails:

t obj; // error: static assertion failed: "should fail"

Finally, if I replace the condition with false, the assertion fails even if I don’t instantiate the class template:

template <typename T>
struct test_assert
{
    static_assert( false, "always fails" );
};

I tried this code on gcc-4.5.1 and gcc-4.7.0. Is this behavior normal? At what time is the compiler supposed to verify static assertions? I guess two-phase lookup is involved, but shouldn’t the typedef trigger the second phase?

  • 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-06T13:16:42+00:00Added an answer on June 6, 2026 at 1:16 pm

    I tried this code on gcc-4.5.1 and gcc-4.7.0. Is this behavior normal?

    Yes

    At what time is the compiler supposed to verify static assertions?

    This is an interesting question. During instantiation, which will be first phase lookup for non-dependent names and second lookup phase for asserts that depend on template arguments.

    guess two-phase lookup is involved, but shouldn’t the typedef trigger
    the second phase?

    Templates are compiled on demand, the typedef just creates an alias to the template and does not trigger the instantiation. Consider the following code:

    template <typename T> class unique_ptr;
    typedef unique_ptr<int> int_unique_ptr;
    

    The template is only declared, but that suffices for the typedef, as it only generates an alias for the type. On the other side, if you create an object of the type, then the template must be instantiated (again on demand, member functions will not be instantiated).

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

Sidebar

Related Questions

While reading this class BitmapFactory I noticed that almost all methods inside are static.
I've noticed that the keyword static in PHP is not that static at all.
While answering Static class variables in Python I noticed that PythonWin PyWin32 build 209.2
I noticed that IE 9 does not support application cache. How can I use
I noticed that in Java, you can use a class which has members of
I was looking at Twitter's static scripts and noticed that all variables and functions
I'm not sure what exactly i'm doing wrong here...but i noticed that my File.Move()
I've noticed that there are many static methods in FCL that affect only single
I noticed that jUnit runs the constructor of my test class for each method
Using java reflection I noticed that all classes run their static constructors when targeted

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.