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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:45:53+00:00 2026-06-15T10:45:53+00:00

GCC allows customization of printf specifiers. However, I don’t see how I can teach

  • 0

GCC allows customization of printf specifiers. However, I don’t see how I can “teach” it to accept my string class for %s specifier. My string class is a simple wrapper over char pointer and has exactly one member variable (char * data) and no virtual functions. So, it’s kind of ok to pass it as-is to printf-like functions in place of regular char *. The problem is that on gcc static analyzer prevents me from doing so and I have to explicitly cast it to const char * to avoid warnings or errors.

My cstring looks something like this:

class cstring
{
   cstring() : data(NULL){}
   cstring(const char * str) : data(strdup(str)){}
   cstring(const cstring & str) : data(strdup(str.data)){}
   ~cstring()
   {
        free(data);
   }
   ...
   const char * c_str() const
   {
        return data;
   }

private:
   char * data;
};

Example code that uses cstring:

cstring str("my string");
printf("str: '%s'", str);

On GCC I get this error:
error: cannot pass objects of non-trivially-copyable type ‘class cstring’ through ‘…’
error: format ‘%s’ expects argument of type ‘char*’, but argument 1 has type ‘cstring’ [-Werror=format]
cc1plus.exe: all warnings being treated as errors

  • 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-15T10:45:53+00:00Added an answer on June 15, 2026 at 10:45 am

    The C++ standard doesn’t require compilers to support this sort of code, and not all versions of gcc support it. (https://gcc.gnu.org/onlinedocs/gcc/Conditionally-supported-behavior.html suggests that gcc-6.0 does, at least – an open question whether it will work with classes such as the one here.)

    The relevant section in the C++11 standard is 5.2.2 section 7:

    When there is no parameter for a given argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by invoking va_arg …
    Passing a potentially-evaluated argument of class type (Clause 9)
    having a non-trivial copy constructor, a non-trivial move constructor,
    or a non-trivial destructor, with no corresponding parameter, is
    conditionally-supported with implementation-defined semantics.

    (But look on the bright side: if you get into the habit of using c_str, then at least you won’t get tripped up when/if you use std::string.)

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

Sidebar

Related Questions

Why won't GCC allow a default parameter here? template<class edgeDecor, class vertexDecor, bool dir>
gcc has a very nice extension in C that allows you to keep data
I was wondering if there is a GCC C Compiler directive that allows me
Is there a gcc macro that allows me to identify whether something is being
I recently found out that gcc allows the definition of nested function. In my
GCC has a statment expression feature that allows macro to be defined as: #define
I haven't yet created a program to see whether GCC will need it passed,
http://gcc.gnu.org/onlinedocs/gcc-2.95.3/objc-features_1.html#SEC2 The GNU Objective-C runtime provides a way that allows you to execute code
Since C+++ allows function overloading, can we overload main() ? For example, int main(const
Can anyone please explain to me, why the compiler allows initialize variables of built-in

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.