我有一些我想弃用的TypeDef。我这样做是为了保持与已存在的代码的向后兼容性。是否有一个优雅(或可能不那么优雅)的解决方案?我希望它与平台无关,但如果只有Visual Studio解决方案,那也可以。
答案 0 :(得分:11)
在MSVC ++中,您可以弃用typedef
,如下所示:
typedef __declspec(deprecated) int myint;
MSVC ++编译器将生成警告,myint
已被弃用!
如果您希望编译器在编译不推荐的typedef时生成特定的消息,请执行以下操作:
typedef __declspec(deprecated("myint is deprecated, so most likely in the next version this myint will be missing")) int myint;
答案 1 :(得分:4)
如果允许一次性简单代码更改,您只需将typedef移动到deprecated
命名空间,该命名空间需要在使用typedef的点使用using namespace deprecated
。
如果那不是一个选项,可能可以编制一个模板,该模板在实例化时会生成警告,但我不知道如何随意生成这样的警告:
template <class T>
class TypedefHolder;
template <>
class TypedefHolder<int>
{
typedef int WhateverType;
// Something that induces a compile warning.
};
所以代替:
typedef int WhateverType;
它变成了:
typedef TypedefHolder<int>::WhateverType WhateverType;
答案 2 :(得分:0)
我知道这个问题已经有 10 年的历史了,但是对于仍然问这个问题的人来说,这种语法已经得到支持since C++14:
[[deprecated("reason")]] typedef int type;