C++ vs. C++/CLI: Const qualification of virtual function parameters

前端 未结 2 642
再見小時候
再見小時候 2020-12-16 19:46

[All of the following was tested using Visual Studio 2008 SP1]

In C++, const qualification of parameter types does not affect the type of a function (8.3.5/3: "

相关标签:
2条回答
  • 2020-12-16 20:00

    It's a bug, and it's not specific to C++/CLI.

    https://connect.microsoft.com/VisualStudio/feedback/details/100917/argument-const-ness-is-part-of-member-function-type-signature

    Fact is, the C++ compiler is supposed to strip off top-level const/volatile. Only const/volatile on the pointed-to type of a pointer or reference matters. If the compiler did that correctly, the CLR wouldn't have a say in what's going on.

    BTW this is the IL generated by the compiler with /clr:pure

    .class private abstract auto ansi beforefieldinit Base
        extends [mscorlib]System.Object
    {
        .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
        {
            .maxstack 1
            L_0000: ldarg.0 
            L_0001: call instance void [mscorlib]System.Object::.ctor()
            L_0006: ret 
        }
    
        .method public hidebysig newslot abstract virtual instance void Foo(int32 modopt([mscorlib]System.Runtime.CompilerServices.IsConst)) cil managed
        {
        }
    
    }
    
    .class private auto ansi beforefieldinit Derived
        extends Base
    {
        .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
        {
            .maxstack 1
            L_0000: ldarg.0 
            L_0001: call instance void Base::.ctor()
            L_0006: ret 
        }
    
        .method public hidebysig virtual instance void Foo(int32 i) cil managed
        {
            .maxstack 0
            L_0000: ret 
        }
    
    }
    

    This definitely violates the rule James listed concerning deletion of top-level qualifiers.

    Further relevant sections of the C++/CLI spec:

    8.8.10.1 Function overriding

    [snip]

    1. A derived class function explicitly overrides a base class virtual function having the same name, parameter-type-list, and cv-qualification, by using the function modifier override, with the program being ill-formed if no such base class virtual function exists

    12.3 Declarator types

    The C++ Standard (§8.3.5/3) is augmented, as follows:
    The resulting list of transformed parameter types and the presence or absence of the ellipsis is the function’s parameter-type-list.

    So I am led to believe that the rule on deletion of cv-qualifiers applies to C++/CLI as well, because the spec specifically calls out section 8.3.5/3 of ISO Standard C++.

    0 讨论(0)
  • 2020-12-16 20:09

    Well, it's a bug. The const modifiers is emitted into the metadata with the modopt custom modifier. Unfortunately, the C++/CLI language rules do not match the CLI rules. Chapter 7.1.1 of the CLI spec says:

    Custom modifiers, defined using modreq (“required modifier”) and modopt (“optional modifier”), are similar to custom attributes (§21) except that modifiers are part of a signature rather than being attached to adeclaration. Each modifer associates a type reference with an item in the signature.

    The CLI itself shall treat required and optional modifiers in the same manner. Two signatures that differ only by the addition of a custom modifier (required or optional) shall not be considered to match. Custom modifiers have no other effect on the operation of the VES.

    So, the CLR says that Derived::Foo() is not a override, C++/CLI says it is. The CLR wins.

    You could report the bug at connect.microsoft.com but it probably a waste of time. I think this incompatibility was intentional. They should have changed the language rules for C++/CLI but surely thought C++ compatibility to be more important. CV modifiers are a pain anyway, there are other scenarios that are not well supported, const pointers to const for one. This cannot be enforced at runtime anyway, the CLR has no support for it.

    0 讨论(0)
提交回复
热议问题