Poco::AutoPtr 上的 const 指针

const pointer on Poco::AutoPtr

尝试使用 AutoPtr 处理 const 指针:

void LiveTimeConfig::setup(const AbstractConfiguration& cfg){
    GetLogger().information("LiveTimeConfig::setup(): init data");
    Poco::AutoPtr<const AbstractConfiguration> view = 
    cfg.createView("live_time");
    try {
       readExist(view, Field::Token, _token);
       readExist(view, Field::Solt, _solt);
    } catch (Poco::SyntaxException& ) {
         GetLogger().error("LiveTimeConfig::setup(): bad values");
         throw Poco::RuntimeException("LiveTimeConfig::setup(): invalid format");
    }
}

但是我在比较运算符上有错误。

../../../build/pc/include/Poco/AutoPtr.h: In instantiation of ‘class Poco::AutoPtr<const Poco::Util::AbstractConfiguration>’: src/LiveTimeConfig.cpp:27:54:   required from here ../../../../build/pc/include/Poco/AutoPtr.h:263:7: error: ‘bool Poco::AutoPtr<C>::operator==(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded   bool operator == (C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:258:7: error: with ‘bool Poco::AutoPtr<C>::operator==(const C*) const [with C = const Poco::Util::AbstractConfiguration]’   bool operator == (const C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:278:7: error: ‘bool Poco::AutoPtr<C>::operator!=(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded   bool operator != (C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:273:7: error: with ‘bool Poco::AutoPtr<C>::operator!=(const C*) const [with C = const Poco::Util::AbstractConfiguration]’   bool operator != (const C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:293:7: error: ‘bool Poco::AutoPtr<C>::operator<(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded   bool operator < (C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:288:7: error: with ‘bool Poco::AutoPtr<C>::operator<(const C*) const [with C = const Poco::Util::AbstractConfiguration]’   bool operator < (const C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:308:7: error: ‘bool Poco::AutoPtr<C>::operator<=(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded   bool operator <= (C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:303:7: error: with ‘bool Poco::AutoPtr<C>::operator<=(const C*) const [with C = const Poco::Util::AbstractConfiguration]’   bool operator <= (const C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:323:7: error: ‘bool Poco::AutoPtr<C>::operator>(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded   bool operator > (C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:318:7: error: with ‘bool Poco::AutoPtr<C>::operator>(const C*) const [with C = const Poco::Util::AbstractConfiguration]’   bool operator > (const C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:338:7: error: ‘bool Poco::AutoPtr<C>::operator>=(C*) const [with C = const Poco::Util::AbstractConfiguration]’ cannot be overloaded   bool operator >= (C* ptr) const
       ^ ../../../../build/pc/include/Poco/AutoPtr.h:333:7: error: with ‘bool Poco::AutoPtr<C>::operator>=(const C*) const [with C = const Poco::Util::AbstractConfiguration]’   bool operator >= (const C* ptr) const

如何为 const AbstractConfiguration*

创建 AutoPtr

UPD

如果使用

const Poco::AutoPtr<AbstractConfiguration> view = cfg.createView("live_time");

错误是

/home/evgen/projects/AuthService/Core/Module/src/LiveTimeConfig.cpp:27: error: invalid conversion from ‘const Poco::Util::AbstractConfiguration*’ to ‘Poco::Util::AbstractConfiguration*’ [-fpermissive]
 const Poco::AutoPtr<AbstractConfiguration> view = cfg.createView("live_time");
                                                                             ^

我不知道这个库,也不知道它的内部结构,但它似乎在内部对 const 类型进行操作时遇到问题。但这应该不是问题,因为,正如来自 header 的那样,get 函数有两个重载,一个用于 const 对象实例 returns const指针。 (const C * get() const;) 所以你可以使用:

const Poco::AutoPtr<AbstractConfiguration> view = 
cfg.createView("live_time");

作为指向 const AbstractConfiguration.

的指针

编辑:

这个编译器错误的原因也可以从这个头文件中读取。 因此,AutoPtr class 为每个运算符 == 声明了一对函数,等等......

bool operator == (
    const C * ptr
) const;

bool operator == (
    C * ptr
) const;

但是当您声明 Poco::AutoPtr<const AbstractConfiguration> 时,两个重载具有相同的声明

bool operator == (
    const AbstractConfiguration * ptr
) const;

调用编译错误。

Minimal example

仅使用 const_cast 成功构建和工作,但我认为它是 hack 和 smell 代码。

const Poco::AutoPtr<AbstractConfiguration> view = const_cast<AbstractConfiguration*>(cfg.createView("live_time"));

Poco::AutoPtr 假定引用计数,即修改对象内的计数器。这与对象的 const

冲突