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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T10:59:55+00:00 2026-05-28T10:59:55+00:00

I’m aware of this question which mentions Boost’s STATIC WARNING, but I’d like to

  • 0

I’m aware of this question which mentions Boost’s “STATIC WARNING”, but I’d like to ask again, specifically, how I could implement a static_warning which operates similarly to static_assert but only emits a warning at compile time rather than an aborting compilation error.

I’d like something similar to Alexandrescu’s proposal for a static assert in pre-C++11 days which somehow managed to print some useful contextual information as part of the error.

It would be acceptable to require that the user enable certain standard compiler warnings in order for this construction to work (perhaps “invalid pointer conversion” or “breaks strict aliasing rules”) — any warning that should be part of a normal compilation anyway can be used.

In short, I want static_warning(false, "Hello world"); to create a compiler warning that should somehow include the string “hello world” in the warning message. Is this possible, say in GCC and MSVC, and how?

I’d happily give out a small reward bounty for any particularly clever solution.


As a bit of explanation: I got the idea when thinking about this question: A static warning would be a useful way to trace through the compile-time process of complex template specializations, which are otherwise fairly hard to debug. A static warning could be used as a simple beacon for the compiler to emit “I’m now compiling this part of the code.”


Update. Ideally, the warning would be triggered in the following setup:

template <typename T> struct Foo
{
    static_warning(std::is_pointer<T>::value, "Attempting to use pointer type.");
    // ...
};

int main() { Foo<int> a; Foo<int*> b; }
  • 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-28T10:59:56+00:00Added an answer on May 28, 2026 at 10:59 am

    Playing off of Michael E’s comment:

    #if defined(__GNUC__)
    #define DEPRECATE(foo, msg) foo __attribute__((deprecated(msg)))
    #elif defined(_MSC_VER)
    #define DEPRECATE(foo, msg) __declspec(deprecated(msg)) foo
    #else
    #error This compiler is not supported
    #endif
    
    #define PP_CAT(x,y) PP_CAT1(x,y)
    #define PP_CAT1(x,y) x##y
    
    namespace detail
    {
        struct true_type {};
        struct false_type {};
        template <int test> struct converter : public true_type {};
        template <> struct converter<0> : public false_type {};
    }
    
    #define STATIC_WARNING(cond, msg) \
    struct PP_CAT(static_warning,__LINE__) { \
      DEPRECATE(void _(::detail::false_type const& ),msg) {}; \
      void _(::detail::true_type const& ) {}; \
      PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \
    }
    
    // Note: using STATIC_WARNING_TEMPLATE changes the meaning of a program in a small way.
    // It introduces a member/variable declaration.  This means at least one byte of space
    // in each structure/class instantiation.  STATIC_WARNING should be preferred in any 
    // non-template situation.
    //  'token' must be a program-wide unique identifier.
    #define STATIC_WARNING_TEMPLATE(token, cond, msg) \
        STATIC_WARNING(cond, msg) PP_CAT(PP_CAT(_localvar_, token),__LINE__)
    

    The macro can be invoked at namespace, structure, and function scope. Given the input:

    #line 1
    STATIC_WARNING(1==2, "Failed with 1 and 2");
    STATIC_WARNING(1<2, "Succeeded with 1 and 2");
    
    struct Foo
    {
      STATIC_WARNING(2==3, "2 and 3: oops");
      STATIC_WARNING(2<3, "2 and 3 worked");
    };
    
    void func()
    {
      STATIC_WARNING(3==4, "Not so good on 3 and 4");
      STATIC_WARNING(3<4, "3 and 4, check");
    }
    
    template <typename T> struct wrap
    {
      typedef T type;
      STATIC_WARNING(4==5, "Bad with 4 and 5");
      STATIC_WARNING(4<5, "Good on 4 and 5");
      STATIC_WARNING_TEMPLATE(WRAP_WARNING1, 4==5, "A template warning");
    };
    
    template struct wrap<int>;
    

    GCC 4.6 (at default warning level) produces:

    static_warning.cpp: In constructor ‘static_warning1::static_warning1()’:
    static_warning.cpp:1:1: warning: ‘void static_warning1::_(const detail::false_type&)’ 
        is deprecated (declared at static_warning.cpp:1): Failed with 1 and 2 [-Wdeprecated-declarations]
    static_warning.cpp: In constructor ‘Foo::static_warning6::static_warning6()’:
    static_warning.cpp:6:3: warning: ‘void Foo::static_warning6::_(const detail::false_type&)’
        is deprecated (declared at static_warning.cpp:6): 2 and 3: oops [-Wdeprecated-declarations]
    static_warning.cpp: In constructor ‘func()::static_warning12::static_warning12()’:
    static_warning.cpp:12:3: warning: ‘void func()::static_warning12::_(const detail::false_type&)’ 
        is deprecated (declared at static_warning.cpp:12): Not so good on 3 and 4 [-Wdeprecated-declarations]
    static_warning.cpp: In constructor ‘wrap<T>::static_warning19::static_warning19() [with T = int]’:
    static_warning.cpp:24:17:   instantiated from here
    static_warning.cpp:19:3: warning: ‘void wrap<T>::static_warning19::_(const detail::false_type&) [with T = int]’ 
        is deprecated (declared at static_warning.cpp:19): Bad with 4 and 5 [-Wdeprecated-declarations]
    

    While Visual C++ 2010 (at /W3 or above) says:

    warnproj.cpp(1): warning C4996: 'static_warning1::_': Failed with 1 and 2
    warnproj.cpp(1) : see declaration of 'static_warning1::_'
    warnproj.cpp(6): warning C4996: 'Foo::static_warning6::_': 2 and 3: oops
    warnproj.cpp(6) : see declaration of 'Foo::static_warning6::_'
    warnproj.cpp(12): warning C4996: 'func::static_warning12::_': Not so good on 3 and 4
    warnproj.cpp(12) : see declaration of 'func::static_warning12::_'
    warnproj.cpp(19): warning C4996: 'wrap<T>::static_warning19::_': Bad with 4 and 5
        with
        [
            T=int
        ]
    warnproj.cpp(19) : see declaration of 'wrap<T>::static_warning19::_'
        with
        [
            T=int
        ]
    warnproj.cpp(19) : while compiling class template member function 'wrap<T>::static_warning19::static_warning19(void)'
        with
        [
            T=int
        ]
    warnproj.cpp(24) : see reference to class template instantiation 'wrap<T>::static_warning19' being compiled
        with
        [
            T=int
        ]
    

    Clang++ 3.1 on Linux produces the arguably nicer output (color not shown):

    tst3.cpp:1:1: warning: '_' is deprecated: Failed with 1 and 2
          [-Wdeprecated-declarations]
    STATIC_WARNING(1==2, "Failed with 1 and 2");
    ^
    tst3.cpp:24:38: note: expanded from macro 'STATIC_WARNING'
      PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \
                                         ^
    tst3.cpp:6:3: warning: '_' is deprecated: 2 and 3: oops
          [-Wdeprecated-declarations]
      STATIC_WARNING(2==3, "2 and 3: oops");
      ^
    tst3.cpp:24:38: note: expanded from macro 'STATIC_WARNING'
      PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \
                                         ^
    tst3.cpp:12:3: warning: '_' is deprecated: Not so good on 3 and 4
          [-Wdeprecated-declarations]
      STATIC_WARNING(3==4, "Not so good on 3 and 4");
      ^
    tst3.cpp:24:38: note: expanded from macro 'STATIC_WARNING'
      PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \
                                         ^
    tst3.cpp:19:3: warning: '_' is deprecated: Bad with 4 and 5
          [-Wdeprecated-declarations]
      STATIC_WARNING(4==5, "Bad with 4 and 5");
      ^
    tst3.cpp:24:38: note: expanded from macro 'STATIC_WARNING'
      PP_CAT(static_warning,__LINE__)() {_(::detail::converter<(cond)>());} \
                                         ^
    tst3.cpp:23:17: note: in instantiation of member function
          'wrap<int>::static_warning19::static_warning19' requested here
    template struct wrap<int>
                    ^
    4 warnings generated.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.