0

我想知道以下语法是否可以“接受”,或者良好实践是否认为这是来自地狱。目标是增加一定程度的保护,以迫使开发人员清楚地意识到自己在做什么。这是语法:

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;
};

用途是:

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

我发现这是告诉开发人员的一种方便方式:“当心,您使用的是低级功能,您必须小心您正在做的事情”。以某种“保护”提供对班级成员的直接访问是一种“可接受的”方式吗?还有其他编码方式吗?

4

1 回答 1

2

看看meagar 的评论

你让你的代码变得丑陋,更难维护和不方便......究竟是什么?定义你的界面。那是你的类的接口。不要让开发人员通过使用一些荒谬的模板标志黑客来绕过它。如果你正在编写代码,你总是必须知道你在做什么。必须明确输入<true>以表明您特别知道自己在做什么,这只是……非常非常错误的想法。开发人员有文档。他们不需要训练轮和人为的限制,他们需要清晰简洁的代码来让他们完成工作。– 米加 2012-10-06 02:41:53Z

当另一个用户使用它时,你提供给其他人的类永远不会进入不可预测的状态。在这种情况下,不可预测的状态是您在编写课程时从未考虑过的状态。因此,您不应该 允许访问 您的类的低级方法或记录可能的缺陷

假设您正在编写一个记录器:

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;
};

忽略没有复制构造函数并且缺少赋值操作数。也忽略它是一个粗略的记录器,它甚至不提供时间戳。但是,如您所见,记录器的状态完全取决于记录器的方法,例如,如果文件已成功打开,则在记录器被销毁之前它不会关闭。

现在说我们使用您的方法:

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;
};

现在有人使用以下代码

logger.writeStream<true>.close();

砰。你的记录器坏了。当然是用户的错,因为他们使用<true>了,不是吗?但用户通常会复制示例代码,尤其是当他第一次使用库时。用户看到你的例子

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

并首先完全忽略文档。然后他将使用第一个和最后一个版本。后来他发现最后一个版本每次都有效!这是多么神奇的事情<true>。然后他开始指责你发生了邪恶的事情,所以你已经通过包含警告的文档保护自己免受公然火焰的伤害:

    /**
    * \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;
    }

那么,我们得到了什么?我们仍然不得不把这个警告放在某个地方。如果我们公开它会简单得多stream

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;
};

或坚持

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

哎呀。因此,要么不允许访问您的低级方法(std::ofstream如果应该允许使用它们,则为特定方法构建一个包装器)或记录如果您对对象的内部进行大量更改时可能发生的缺陷,但不要走中间路线,用static_assert.

于 2012-10-06T03:23:04.717 回答