微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

这是超载吗?

如何解决这是超载吗?

如果我有两个这样的函数

form.is_valid() or form.errors: # form.is_valid() is perferred way
    form.full_clean() =
        for field in form.fields:
            field_name = field.field_name
            field.clean() =
                try:
                    form.cleaned_data[field_name] = field.to_python() =
                            # Accepts raw value from widget and coerces it. E.g. A FloatField will turn the data into a Python float or raise a Validation Error.
                            return cleaned_value := (coerce)value
                    field.validate() =
                        # *Override this
                        # For custom field-specific validation that is not suitable for a validator (which tend to be generic).
                        # Should not alter the value.
                        if not custom_validation():
                            raise ValidationError()
                        return # Does not return anything
                    field.run_validators() =
                        # Should not need to override this
                        errors = []
                        try:
                            for validator in field.validators:
                                validator.validate()
                        except ValidationError as e:
                            errors += e
                    form.cleaned_data[field_name] = form.clean_<field_name>(self) =
                        # *Override this
                        # Custom field cleaning.
                        # Must return a value,as it replaces the value in cleaned_data.
                        cleaned_value = self.cleaned_data[field_name]
                        custom_cleaned_value = custom_clean(cleaned_value)
                        return custom_cleaned_value
                except ValidationError:
                    continue
        form.clean(self)
            # *Override this
            # Place custom validation that depends on multiple fields here.
            # Have access to the forms errors here
            # Validation errors raised here will not be associate with any field,they go into a special field called __all__,which can be accessed via the Form.non_field_errors() method.
            # However one can attach a certain error to a specific field with Form.add_error().
            if isinstance(form,forms.ModelForm):
                form.flag_for_model_validation_to_check_unique_fields = True
            if field_A == 'foo' and field_B == 'bar':
                self.add_error('foo',ValidationError('Field A must not be foo if field B is bar')) # add error removes the field from cleaned_data
                self.add_error('bar',ValidationError('Field B must not be bar if field A is foo'))
    if isinstance(form,forms.ModelForm):
        Model.full_clean() =
            # Not called when calling a models save() method.
            Model.clean_fields(exclude=None) = 
                # This method will validate all fields on your model.
                # The optional exclude argument lets you provide a list of field names to exclude from validation.
                # It will raise a ValidationError if any fields fail validation.
                for field in model.fields:
                    field.clean()
            Model.clean()
                # *Override this but call super().
                cleaned_data = super().clean() # see note about flag_for_model_validation_to_check_unique_fields above
                # Not called when calling a models save() method.
                # This method should be overridden to perform custom validation on your model.
                # This method should be used to provide custom model validation,and to **modify attributes on your model if desired**.
            Model.validate_unique(exclude=None)
                # Lastly checks any unique constraints on your model.
                # This method is similar to clean_fields(),but validates all uniqueness constraints on your model instead of individual field values.

他们超载了吗?

int f ( int a ) {} void f ( signed a ) {} 调用 main() 时,出现错误

f(5)

解决方法

类型说明符 signedint(单独使用,不带其他类型说明符)定义相同的有符号整数类型。

也就是说你可以等效地写例如

int a;
signed a;
signed int a;
int signed a;

所以问题中的函数具有相同的参数声明。

但是它们有不同的返回类型

int f ( int a )
{ }

void f ( signed a)
{}

即同一个函数被重新定义为不同的返回类型。返回类型不参与函数重载。

所以编译器会报错,同一个函数用不同的返回类型定义了两次。

来自 C++ 14 标准(13.1 可重载声明)

2 某些函数声明不能​​重载:

(2.1) — 仅返回类型不同的函数声明 不能重载

如果您要更改其中一个函数中的参数声明,例如以下方式

int f ( int a )
{ }

void f ( unsigned a)
{}

然后函数将被重载和这个语句

f( 5 );

将调用第一个重载函数,因为整数文字 5 的类型为 int..

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。