Clang error: conversion function cannot have a return type (err_conv_function_return_type)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: conversion function cannot have a return type
Type Error
Category Semantic Issue
Internal Id err_conv_function_return_type
Internal Message conversion function cannot have a return type
Regular Expression (?:error|fatal error)\: conversion function cannot have a return type
First Commit 2009-03-14 5a8987ca5113 Update tablegen diagnostic files to be in sync with the def files.

Description

The error is issued by the Clang compiler when it encounters a declaration of a conversion function that incorrectly includes a return type. In C++, conversion functions are special member functions that enable object type conversion. These functions must be declared without specifying a return type, as the target type of the conversion is indicated by the function's name itself following the operator keyword. This issue typically arises from a misunderstanding of the correct syntax for declaring conversion functions.

For a conversion function to be valid, it should be declared within the body of a class or struct without a return type, and the function name should be prefixed with the operator keyword followed by the type to which the conversion is performed. The presence of a return type in the declaration is syntactically incorrect and violates the C++ standards governing conversion functions, prompting the Clang compiler to generate this error message.

Fixing this error involves removing the return type from the conversion function declaration and ensuring that the correct syntax as per C++ standards is followed. This correction aids in maintaining the correct semantics of conversion functions, allowing for their intended use in type conversions within C++ programs.

 
AI Generated

Example

In the following example, a struct named S is defined with a member function intended to serve as a conversion operator to the int type. However, the function declaration mistakenly includes a return type of int, which is syntactically incorrect for a conversion function in C++. Conversion functions in C++ should not explicitly state a return type because their purpose is to define how an object of the struct or class can be converted to another type, which is implicitly understood from the function’s name following the operator keyword. The correct way to declare a conversion function is to omit the return type entirely and specify only the type to which the conversion is made after the operator keyword. The error message from the compiler highlights this syntax issue, indicating that the declaration of the conversion function does not follow the standard C++ convention.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
struct S { 
  // Invalid conversion function 
  int operator int() const { return 0; } 
};
Compiler Output
<source>:3:7: error: conversion function cannot have a return type


Clang Internals (17.0.6)

Git Commit Message

Update tablegen diagnostic files to be in sync with the def files.

llvm-svn: 67004

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaDeclCXX.cpp (line 11010)

/// CheckConversionDeclarator - Called by ActOnDeclarator to check the
/// well-formednes of the conversion function declarator @p D with
/// type @p R. If there are any errors in the declarator, this routine
/// will emit diagnostics and return true. Otherwise, it will return
/// false. Either way, the type @p R will be updated to reflect a
/// well-formed type for the conversion operator.
void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, StorageClass &SC) {
  // ...
  if (DS.hasTypeSpecifier() && !D.isInvalidType()) {
    // ...
    Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) << SourceRange(DS.getTypeSpecTypeLoc()) << SourceRange(D.getIdentifierLoc());

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/SemaCXX/conversion-function.cpp

  • clang/test/SemaCXX/conversion-function.cpp:31:8: error: conversion function cannot have a return type