Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct syntax for deprecating a single constructor in Visual Studio C++?

I am using the new [[deprecated("message")]] attributes in my code. I have a class that has two constructors, one of which should be marked as deprecated (obviously simplified, and possibly with syntax errors):

class MyClass
{
public:
    // good constructor
    MyClass(int someNumber): _someNumber(someNumber) {}

    [[deprecated("Use MyClass(int) instead")]]
    MyClass(const char* someStr): _someNumber(atoi(someStr)) {}

private:
    int _someNumber;
}

Visual Studio complains that "attribute 'deprecated("Use MyClass(int) instead")' cannot be applied in this context.

Is there a way to deprecate a constructor in Visual Studio 2015 so that I get a warning if it is used anywhere?

like image 307
Ben Avatar asked Oct 24 '25 08:10

Ben


1 Answers

Mark the parameter as deprecated:

MyClass([[deprecated]]const char* someStr){}

Or:

MyClass([[deprecated("Use MyClass(int) instead")]]const char* someStr){}

Or use __declspec(deprecated):

__declspec(deprecated("** Use MyClass(int) instead **"))
        MyClass(const char* someStr) : _someNumber(atoi(someStr)) {}

Also depending on your update version (no. 3) you might be experiencing this bug.

like image 72
Ron Avatar answered Oct 26 '25 22:10

Ron