1

I have a function:

template<std::same_as<int&> T> void f(T value) {} 

Compiler gives me an error that I don't have a function with these arguments f(int) if I call it like this:

void g() { int a; int& b = a; f(b); } 

Why does the compiler assume that I use a regular type and not a reference and is there something I can do about it?

1 Answer 1

2

Unless you have a forwarding reference, the template type is never deduced as a reference type. Instead, it gets the type the reference refers to. In this case that means T is deduced as an int and that does not match int&. Instead you can use

template<std::same_as<int> T> void f(T& value) {} 

Which forces T to be an int, and then T& makes value a reference to the object passed to the function.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.