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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:49:05+00:00 2026-06-08T13:49:05+00:00

I have a macro that I would only like to be called at file

  • 0

I have a macro that I would only like to be called at file scope. It would be great if it could complain somehow (at compile time, or at runtime) if it is used within a function. Is this possible?

EDIT: To respond to comments so far: I have static objects that are used repeatedly in my codebase. I have created macros to make creating these objects faster to type, as these objects are used all over the place, to the point where saving keystrokes matters for productivity / annoyance.

Up until this point, I had been creating these objects as static within functions. Recently discovered that this is not thread safe. I’m deciding to move all of these objects out of functions so they are created at program start rather than lazily. I just thought it might be nice to have a failsafe to prevent this bug from popping up again in the future.

To be clear- this isn’t holding back progress at all. It’s just something I thought would be a nice addition to the macro if it is easy / possible.

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

    What you’re looking for is a construct that works at the file level but not at the function level. Off the top of my head, namespaces should fit the bill.

    #define FOO(x) namespace { x }
    
    FOO(int bar = 42;) // Compiles
    
    void f() {
        FOO(int bar = 42;) // Doesn't compile
    }
    

    Since I don’t know exactly what you’re trying to wrap, I kept this macro as simple as possible. Any actual implementation should use the standard macro creation tricks to make sure it’s nice and safe.

    Note that anonymous namespaces will make it so every object created within it are only visible to the current compilation unit. In your case, this should not be an issue because your object used to be declared at the function level and were therefor not visible outside the current compilation unit.

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

Sidebar

Related Questions

I have an application that used Macro. In this macro I would like to
I would like to export some macros that I have defined and be able
I have a macro that passes the line number and file name to an
I have a macro setup to automatically open/save a file that I am opening
I have a macro that iterates through some rows, to update the colouring of
I am trying to have a macro that will do some formatting. I need
I have an umbraco macro that spits out the content of the page at
I have a VBA Word Macro that gets words from .txt list and color
I have created a macro for excel which will pop up form that contains
I have an application that writes many times to a formula/macro-laden workbook. It loops

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.