llvm / llvm/llvm-project

C++ function overloading bug

Open
#175,294 8 comments 0 reactions 0 assignees View on GitHub
clang:frontend
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The C++ function overloading worked incorrectly when calling a overloading function with a const sring parameter.
The compiler matched a function with a const string parameter incorrectly to a function with a bool parameter.
Both clang++ and g++ have this problem.

Here is the source codes.

//test.cpp
#include
#include

class A{
public:
A(std::string name){
std::cout << "Constructor A(string)" << std::endl;
}

A(bool b){
std::cout << "Constructor A(bool)" << std::endl;
}

void f1(std::string name){
std::cout << "f1(string)" << std::endl;
}

void f1(bool b){
std::cout << "f1(bool)" << std::endl;
}
};

int main()
{
const std::string s = "sss";
A a1(s); //correct
A a2("sss"); //wrong!!!
A a3(true); //correct

a1.f1(s); //correct
a1.f1("sss"); //wrong!!!
a1.f1(true); //correct
}

//g++ test.cpp -o test
//clang++ test.cpp -o test

/*

$ g++ --version
g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ clang++ --version
Ubuntu clang version 18.1.3 (1ubuntu1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

*/

The results of the codes should be :
Constructor A(string)
Constructor A(string)
Constructor A(bool)
f1(string)
f1(string)
f1(bool)

instead of:
Constructor A(string)
Constructor A(bool)
Constructor A(bool)
f1(string)
f1(bool)
f1(bool)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.