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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:11:06+00:00 2026-06-12T14:11:06+00:00

I wonder if the following syntax can be admitted or if good practices consider

  • 0

I wonder if the following syntax can be “admitted” or if good practices consider this as coming from hell. The goal would be to add a level of protection to force the developer to be well-conscious of what he is doing. Here is the syntax :

class MyClass
{
    public:
        template<bool RemoveProtection = false> 
        inline std::ofstream& writeStream()
        {
            static_assert(RemoveProtection, "You're doing it wrong");
            return _writeStream;
        }

        inline const std::ofstream& writeStream() const
        {
            return _writeStream;
        }

    protected:
        std::ofstream _writeStream;
};

The use would be :

x.writeStream().good(); // <- OK
x.writeStream().put('c'); // <- NOT OK
x.writeStream<true>().put('c'); // <- OK

I find this as a convenient way to tell to the developer : “Beware, you are using a low-level function and you have to be careful with what you are doing”. Is it a “acceptable” way of providing a direct access to class members with a kind of “protection” ? Is there other way of coding a such thing ?

  • 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-12T14:11:07+00:00Added an answer on June 12, 2026 at 2:11 pm

    Have a look at meagar’s comment:

    You’re making your code ugly, harder to maintain and inconvenient for… what exactly? Define your interface. That is the interface to your class. Do not allow developers to bypass it by using some ridiculous template flag hackery. If you’re writing code, you always have to know what you’re doing. Having to explicitly type <true> to indicate you especially know what you’re doing is just… very, very wrong-headed. Developers have documentation. They don’t need training wheels and artificial restrictions, they need clear concise code that lets them get things done. – meagar 2012-10-06 02:41:53Z

    A class you provide to others should never be able to get into an unpredicted state when another user uses it. An unpredicted state is in this case a state which you never considered when you were writing the class. As such you should either never allow access to low-level methods of your class or document possible flaws.

    Lets say you’re writing a logger:

    struct MyLogger{
        MyLogger(std::string filename) : stream(filename.c_str()){}      
        template <typename T>
        MyLogger& operator<<(const T& v){ stream << v << " "; return *this;}
    
    private:
        std::ofstream stream;
    };
    

    Ignore that there isn’t a copy constructor and that the assignment operand is missing. Also ignore that it’s a crude logger, it doesn’t even provide a timestamp. However, as you can see, the state of the logger depends completely on the logger’s methods, e.g. if the file has been successfully opened it won’t be closed until the logger gets destroyed.

    Now say that we use your approach:

    struct MyLogger{
        MyLogger(std::string filename) : stream(filename.c_str()){}
        template <typename T>
        MyLogger& operator<<(const T& v){ stream << v << " "; return *this;}
    
        template<bool RemoveProtection = false> 
        inline std::ofstream& writeStream()
        {
            static_assert(RemoveProtection, "You're doing it wrong");
            return stream;
        }
    
        inline const std::ofstream& writeStream() const
        {
            return stream;
        }
    
    private:
        std::ofstream stream;
    };
    

    And now someone uses the following code

    logger.writeStream<true>.close();
    

    Bang. Your logger is broken. Of course it’s the users fault, since they used <true>, didn’t they? But more often than not a user is going to copy example code, especially if he uses a library for the first time. The user sees your example

    logger.writeStream().good(); // <- OK
    logger.writeStream().put('c'); // <- NOT OK
    logger.writeStream<true>().put('c'); // <- OK
    

    and ignores the documentation completely at first. Then he’s going to use the first and the last version. Later he discovers that the last version works every time! What a miraculous thing <true> is. And then he starts to blame you for evil things that happen, so you have protect yourself from blatant flames with a documentation which includes a warning:

        /**
        * \brief Returns a reference to the internal write stream
        *
        * \note You have to use the template parameter `<true>` in order to use it
        *
        * \warning Please note that this will return a reference to the internal 
        *          write stream. As such you shouldn't create any state in which 
        *          the logger cannot work, such as closing the stream.        
        */
        template<bool RemoveProtection = false> 
        inline std::ofstream& writeStream()
        {
            static_assert(RemoveProtection, "You're doing it wrong");
            return stream;
        }
    

    So, what did we get? We still had to put that warning somewhere. It would have been much simpler if we made stream public:

    struct MyLogger{
        MyLogger(std::string filename) : stream(filename.c_str()){}
        template <typename T>
        MyLogger& operator<<(const T& v){ stream << v << " "; return *this;}
    
        /**
        * The internal write stream. Please look out that you don't create
        * any state in which the logger cannot work, such as closing the stream.
        */
        std::ofstream stream;
    };
    

    or sticked to

    /** >put warning here< */
    inline std::ofstream & writeStream()
    {
        return stream;
    }
    

    Woops. So either don’t allow access to your low-level methods (build a wrapper to specific std::ofstream methods if they should be allowed to use them) or document the possible flaws which can happen if you alter the object’s internals to much, but don’t go the middle-way and make it look okay with a static_assert.

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

Sidebar

Related Questions

I wonder if the following would be a good Idea or rather contra-productive performance-wise:
In the following code I am getting this warning from Reshaper. I wonder if
wonder whether someone can help me with the following one... I have a struct
I wonder whether someone can help me please. I'm using the following script to
I wonder, what library you would choose for the following: I need to be
This question led me to wonder the following: What are the 'Gotchas' associated with
I need help with the following wonder if anyone knows what i can do
I'm struggling to compile a query for the following and wonder if anyone can
I wonder how can I reconcile the following error? JVM cannot use large page
I wonder if the following would be a correct implementation ExecutorService pool = Executors.newFixedThreadPool(MAX_NSH_THREADS);

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.