Clang error: conversion function cannot convert to a function type (err_conv_function_to_function)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: conversion function cannot convert to a function type
Type Error
Category Semantic Issue
Internal Id err_conv_function_to_function
Internal Message conversion function cannot convert to a function type
Regular Expression (?:error|fatal error)\: conversion function cannot convert to a function 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 a C++ program includes a conversion function attempting to convert to a function type. In C++, a conversion function is a special member function that is used to convert one type to another. According to the C++ standard, the type to which the conversion function intends to convert (specified by the conversion-type-id in the function's declaration) must not represent a function type or an array type.

When a conversion function is declared with a function type as its target conversion type, it violates this rule because function types are not allowed as target types for conversion functions. This is because allowing a conversion to a function type could introduce ambiguities and complexities in the language semantics that are undesirable.

This specific restriction ensures more straightforward and predictable behavior of conversion functions in C++, preventing a class of potential programming errors and misunderstandings regarding the type conversion semantics.

Clang's diagnostic message for this error aims to guide the developer to adhere to the C++ standards by prohibiting the declaration of conversion functions targeting function types, encouraging the use of alternative design patterns that respect the language's rules for type conversions.

 
AI Generated

Example

In the following example, a structure named S is defined with a conversion operator attempting to convert the structure to a function type. The conversion operator is declared with the syntax operator void(*)(), which signifies an attempt to convert to a pointer to a function returning void and taking no parameters. This declaration violates the C++ standard rule that a conversion function's target type must not be a function type, leading to a compilation error. The compiler errors presented indicate issues with the declaration: a type specifier is required for all declarations, a conversion function cannot have any parameters, part of a return type cannot be specified in the declaration of a conversion function (suggesting the use of a typedef instead), and finally, that a conversion function cannot convert to a function type. This example serves to illustrate the improper use of a conversion function to a function type, which is not permitted in C++.  
AI Generated


Flags -xc++ -std=c++17

[Try out in Compiler Explorer]

Source
struct S {
  // Conversion to function type not allowed
  operator void(*)() {};
};
Compiler Output
<source>:3:18: error: a type specifier is required for all declarations
<source>:3:3: error: conversion function cannot have any parameters
<source>:3:19: error: cannot specify any part of a return type in the declaration of a conversion function; use a typedef to declare a conversion to 'void ()'
<source>:3:3: error: conversion function cannot convert to a function 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 11123)

/// 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) {
  // ...
  // C++ [class.conv.fct]p4:
  //   The conversion-type-id shall not represent a function type nor
  //   an array type.
  if (ConvType->isArrayType()) {
  // ...
  } else if (ConvType->isFunctionType()) {
    Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function);

Triggered in Clang Tests

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

clang/test/SemaCXX/PR31422.cpp

  • clang/test/SemaCXX/PR31422.cpp:6:29: error: conversion function cannot convert to a function type