Function Overloading #20889

Closed
opened 2026-01-31 07:26:55 +00:00 by claunia · 3 comments
Owner

Originally created by @shiyou-t1c on GitHub (Nov 24, 2023).

Windows Terminal version

No response

Windows build number

No response

Other Software

No response

Steps to reproduce

#include <iostream>
int c(const char (&a1)[3])
{
	return 1;
}
int c(const char *a1)
{
	return 3;
}
int main()
{
	char arr[] = "hi";
	std::cout << c(arr) << std::endl;
	return 0;
}

Expected Behavior

No response

Actual Behavior

https://learn.microsoft.com/en-us/cpp/cpp/function-overloading?view=msvc-170

int c(const char (&a1)[3]);           char[3]          const char [3]           const char (&)[3]

int c(const char *a1);                char[3]          char*                         const char*

I don't understand why int c(const char (&a1)[3]); Matching has a higher priority.Sequences of trivial conversions are classified as exact matches.I really don't understand.How to match the priorities after many conversions?

Originally created by @shiyou-t1c on GitHub (Nov 24, 2023). ### Windows Terminal version _No response_ ### Windows build number _No response_ ### Other Software _No response_ ### Steps to reproduce ```c++ #include <iostream> int c(const char (&a1)[3]) { return 1; } int c(const char *a1) { return 3; } int main() { char arr[] = "hi"; std::cout << c(arr) << std::endl; return 0; } ``` ### Expected Behavior _No response_ ### Actual Behavior [https://learn.microsoft.com/en-us/cpp/cpp/function-overloading?view=msvc-170](url) ```c++ int c(const char (&a1)[3]); char[3] const char [3] const char (&)[3] int c(const char *a1); char[3] char* const char* ``` ### **I don't understand why int c(const char (&a1)[3]); Matching has a higher priority.Sequences of trivial conversions are classified as exact matches.I really don't understand.How to match the priorities after many conversions?**
claunia added the Needs-TriageIssue-Bug labels 2026-01-31 07:26:56 +00:00
Author
Owner

@shiyou-t1c commented on GitHub (Nov 24, 2023):

Maybe this is the conversion?

int c(const char (&a1)[3]);           char[3]          const char [3]           const char (&)[3]

int c(const char *a1);                char[3]          const char [3]               const char*

How is this sequence decided?Why is conversion to reference higher than conversion to pointer?

@shiyou-t1c commented on GitHub (Nov 24, 2023): **Maybe this is the conversion?** ```c++ int c(const char (&a1)[3]); char[3] const char [3] const char (&)[3] int c(const char *a1); char[3] const char [3] const char* ``` **How is this sequence decided?Why is conversion to reference higher than conversion to pointer?**
Author
Owner

@DHowett commented on GitHub (Nov 24, 2023):

Hi there! You may have better luck asking this question on Stack Overflow. This repository is for discussing issues in the Windows Terminal application and the Windows Console subsystem. We are not very good at giving generic C++ advice :)

@DHowett commented on GitHub (Nov 24, 2023): Hi there! You may have better luck asking this question on Stack Overflow. This repository is for discussing issues in the Windows Terminal application and the Windows Console subsystem. We are not very good at giving generic C++ advice :)
Author
Owner

@shiyou-t1c commented on GitHub (Nov 25, 2023):

I understand now. Thank you very much.

@shiyou-t1c commented on GitHub (Nov 25, 2023): **I understand now. Thank you very much.**
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20889