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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:25:07+00:00 2026-05-13T09:25:07+00:00

Below is a small test case that demonstrates a problem that I am trying

  • 0

Below is a small test case that demonstrates a problem that I am trying to solve using templates in C++:

template<typename T>
void
unused(T const &) {
  /* Do nothing. */
}

int main() {
  volatile bool x = false;
  unused(!x); // type of "!x" is bool
}

As written below, the g++ v3.4.6 compiler complains:

test.cc: In constructor `test::test()':
test.cc:11: error: invalid initialization of reference of type 'const volatile bool&' from expression of type 'volatile bool'
test.cc:3: error: in passing argument 1 of `void unused(const T&) [with T = volatile bool]'

The goal here is to have unused suppress unused variable warnings that occur in optimized code. I have a macro that does an assertion check and in optimized code the assertion goes away, but I want any variables in the assertion’s expression to remain referenced so that I don’t get unused variable warnings only in optimized code. In the definition for unused() template function, I use a reference so that no copy constructor code gets inadvertently run so that the call to unused can be completely elided by the compiler.

For those interested, the assertion macro looks like this:

#ifdef NDEBUG
#  define Assert(expression) unused(expression)
#else // not NDEBUG
#  define Assert(expression)      \
{         \
  bool test = (expression);      \
     \
  if (!test) {        \
    if (StopHere(__LINE__, __FILE__, __PRETTY_FUNCTION__,  \
                    #expression, false)) {    \
      throw Exit(-1); /* So that destructors are run. */  \
    }         \
  }         \
}
#endif // else not NDEBUG

For the above test case, I can make the error go away by adding another similar unused function like this:

template<typename T>
void
unused(T const) {
  /* Do nothing. */
}

However, then other cases calling unused() fail due to ambiguity when the argument can be made a reference to with something like:

file.h:176: error: call of overloaded `unused(bool)' is ambiguous
myAssert.h:27: note: candidates are: void unused(T) [with T = bool]
myAssert.h:34: note:                 void unused(const T&) [with T = bool]

So my question is, how can I change unused() or overload it so that it meets the following requirements:

  1. The call to unused() can be optimized away into a no-op by the compiler.
  2. It causes any variables that are present in the expression passed to unused() to appear used and thus not result in a warning about them being defined but not used.
  3. The argument to unused() may or may not be able to be referenced.
  4. The argument to unused() may be an object with an expensive copy constructor which should not be invoked when unused() is invoked.

Thanks.

-William

  • 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-13T09:25:07+00:00Added an answer on May 13, 2026 at 9:25 am

    As Johannes said in the comments, you hit a compiler bug. You can work around it by explicitly converting to bool:

    unused( bool( !readWriteActivated) ); // add bool() to any (!volatile_bool_var)
    

    Old answer (but still not a bad idea)

    If I recall the const-volatile qualification rules, all you need is to qualify the dummy variable more. Essentially, you just want to parrot the error message back in the declared type :vP .

    template<typename T>
    void
    unused(T const volatile &) { // only change is to add "volatile"
      /* Do nothing. */
    }
    

    Also, nice that you put the const after the type, where it belongs.

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

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm not sure about other operating systems, but I know… May 14, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer There may be a LINQ method to accomplish this task.… May 14, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer It's good to define a global value, that changes relatively… May 14, 2026 at 7:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.