我有一个纯虚类,其中有一个应该是
我试图使这个方法成为
以下是我目前能够制作的示例:
阶段0:改变之前。只有非const版本的
在阶段1,我希望能够在类重写
const
的纯虚方法,但不幸的是它不是。这个接口在一个库中,这个类被几个其他类继承,在不同的项目中。我试图使这个方法成为
const
,而不会破坏兼容性(至少在一段时间内),但我找不到一种方法,在非const方法重载时生成警告。以下是我目前能够制作的示例:
阶段0:改变之前。只有非const版本的
Interface :: doSomething()
方法存在,并且它是纯虚的。
阶段1:过渡期间。 Interface :: doSomething()
方法既有const版本也有非const版本。它们都有一个默认实现,以允许旧式和新式实现(在这个阶段它们不能是纯虚的,因为每个继承的类只会重写其中一个)。const版本调用非const版本,以确保与旧实现的兼容性,非const版本断言,因为它永远不应该被调用。
阶段2:只有非const版本的Interface :: doSomething()
方法存在,并且它是纯虚的。在阶段1,我希望能够在类重写
Interface::doSomething()
的非const版本时产生警告,以警告用户他们应该更新他们的代码,这样当我切换到阶段2时,破坏其他人代码的机会非常低。不幸的是,我找不到一种方法来做到这一点。我尝试了GCC和Clang的几种标志组合,但唯一能做的就是使编译失败(例如将其更改为final
),但这不是我想处理此问题的方式。有没有一种方法可以生成警告?#include <iostream>
#include <cassert>
class Interface
{
public:
virtual ~Interface() = default;
// callDoSomething method:
// - stage 0: non const
// - stage 1-2: const
#if (STAGE == 0)
void callDoSomething() { doSomething(); }
#else
void callDoSomething() const { doSomething(); }
#endif
protected:
// non-const doSomething() method:
// - stage 0: pure virtual
// - stage 1: virtual with assert in default implementation (should never be called)
// - stage 2: removed
#if (STAGE == 0)
virtual void doSomething() = 0;
#elif (STAGE == 1)
[[deprecated("Overload const version instead")]]
virtual void doSomething()
{
assert(false);
}
#endif
// const doSomething() method
// - stage 0: N/A
// - stage 1: virtual with default implementation (calls the non-const overload)
// - stage 2: pure virtual
#if (STAGE == 1)
virtual void doSomething() const
{
std::cout << __PRETTY_FUNCTION__ << '\n';
std::cout << " calling non const version\n";
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
const_cast<Interface*>(this)->doSomething();
#pragma GCC diagnostic pop
}
#elif (STAGE == 2)
virtual void doSomething() const = 0;
#endif
};
// Old style implementation: non-const doSomething()
// Allowed only in stages 0 and 1
#if (STAGE == 0 || STAGE == 1)
class Implementation_old : public Interface
{
public:
virtual ~Implementation_old() = default;
protected:
virtual void doSomething() override
{
std::cout << __PRETTY_FUNCTION__ << '\n';
}
};
# endif
// Old style implementation: const doSomething()
// Allowed only in stages 1 and 2
#if (STAGE == 1 || STAGE == 2)
class Implementation_new : public Interface
{
public:
virtual ~Implementation_new() = default;
protected:
virtual void doSomething() const override
{
std::cout << __PRETTY_FUNCTION__ << '\n';
}
};
#endif
int main(int argc, char *argv[])
{
Interface* iface = nullptr;
#if (STAGE == 0 || STAGE == 1)
iface = new Implementation_old;
iface->callDoSomething();
delete iface;
#endif
#if (STAGE == 1)
std::cout << "-------------------\n";
#endif
#if (STAGE == 1 || STAGE == 2)
iface = new Implementation_new;
iface->callDoSomething();
delete iface;
#endif
iface = nullptr;
return 0;
}
这是CMakeLists.txt文件,用于使用3个 STAGE
定义构建示例
cmake_minimum_required(VERSION 3.5)
project(test_deprecate_non_const)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(main_stage_0 main.cpp)
target_compile_definitions(main_stage_0 PRIVATE STAGE=0)
add_executable(main_stage_1 main.cpp)
target_compile_definitions(main_stage_1 PRIVATE STAGE=1)
add_executable(main_stage_2 main.cpp)
target_compile_definitions(main_stage_2 PRIVATE STAGE=2)