Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The C++ code below fails to compile as shown. Kindly advise. Thank You
import std.core;
using namespace std;

struct cfoo
{
	using result_type = int;
	template<typename U> requires is_same_v<U, result_type>
	void bar(U);
};
template<typename U> requires is_same_v<U, typename cfoo::result_type>
void cfoo::bar(U) {} //  error C2244: 'cfoo::bar': unable to match function definition to an existing declaration

int main()
{
	return 0;
}


What I have tried:

Not too much. Looked up error C2244 on Microsoft site. Was not helpful.
Posted
Updated 16-May-22 15:21pm
Comments
Greg Utas 16-May-22 20:49pm    
I haven't used this requires is_same<> stuff, but I'm guessing that it shouldn't be repeated in the function definition. Also, if U is a parameter to that function, won't you have to give it name for the function to be useful?
BernardIE5317 16-May-22 21:28pm    
The template "requires" statement needs to be in the definition as well as the declaration of "bar" else it will not compile which it would succeed in doing if it did not reference "cfoo::result_type" e.g. if the "requires" clause for "bar" was instead "requires is_integral_v". Yes a name would be needed for the "bar" "U" parameter for useful execution of course but all I wish to learn is how to obtain successful compilation. - Best

1 solution

I was not able to get this to compile using the Compiler Explorer[^] with MSVC. However, by removing the import directive and adding #include <type_traits> gcc and clang both compiled with no warnings using -Wall -Wextra flags, but I did have to specify -std=c++20. Adding #inlude <type_traits> to the MSVC compile did not improve results, but maybe there are some flags for MSVC that will get MSVC to accept the code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900