802 questions
1
vote
1
answer
133
views
Unexpected overload resolution on clang [duplicate]
(I was making an integer class to replace C's stuff, then this hit me. I wanted an overload for pointer arithmetic.)
MSVC and GCC pass without errors or warnings.
Compilation:
g++ -std=c++2c -fsyntax-...
1
vote
1
answer
142
views
Partial ordering of overloaded function templates seems to fail, why?
Consider the following example:
#include <iostream>
#include <vector>
template<typename T>
void foo( T && )
{
std::cout << "Common foo!\n";
}
template&...
6
votes
3
answers
160
views
Is there a way to rank user-defined conversions between class templates mirror the ranking of conversions of their template arguments?
Consider the following code (live example):
#include <memory>
struct Base {};
struct Middle : public Base {};
struct Derived : public Middle {};
void foo(Base*);
void foo(Middle*);
void ...
5
votes
1
answer
199
views
Construction from nested brace-enclosed initializer list
I have a working program that compiles successfully in Visual Studio. But trying to port it to GCC/Clang resulted in some compilation errors.
Maximally reduced example is as follows:
#include <...
4
votes
2
answers
103
views
Why does class extending raw type result in overload ambiguity?
Why is method(new ExtendsRaw()) ambiguous in the code below?
public class Main {
private Main() {}
static class Generic<T> {}
@SuppressWarnings("rawtypes")
static ...
1
vote
1
answer
67
views
Unable to call desired overload of a generated PInvoke function
I'm am testing out the C#/Win32 project so I can call Win32 Setup API functions with automatically generated PInvoke wrappers. I am unable to make my code call the correct overload of one of the ...
4
votes
1
answer
76
views
Store value category of pointed-to object (for later deref)
Ok this might be a bit difficult to explain:
I want to call a function hello that has an overload for either an rvalue or an lvalue ref of type Json. This Json object is converted from a JsonKey using ...
6
votes
1
answer
104
views
Do template parameter packs affect overload resolution?
Look at this code (godbolt):
template <typename ...T>
void func(const char *it, T &&...t) {}
template <typename A, typename B>
void func(A &&a, B &&b) {}
int main(...
0
votes
1
answer
95
views
Overload resolution through return type?
Function overload resolution doesn't take return type into account, however I came across a piece of code on cppreference
SFINAE aside, how is overload resolution happening here:
#include <iostream&...
14
votes
3
answers
1k
views
Overload resolution of a pointer and a container with pointers
I wrote a function with two overloads - one for a single pointer and one for a vector of pointers. At some point in the code I wanted to pass an empty vector to the function and used {} as the ...
0
votes
1
answer
133
views
how can a standard conversion include a user-defined conversion?
I'm trying to understand the following language from cppreference.com (emphasis mine):
Each type of standard conversion sequence is assigned one of three ranks:
Exact match: no conversion required, ...
1
vote
1
answer
80
views
Overloaded user defined conversion operator based on value category of object selecting counter-intuitive overload
Can someone please explain which language rules are in play in the following example:
#include <iostream>
#include <type_traits>
template<typename T>
struct Holder{
T val;
...
2
votes
0
answers
65
views
Confusing overload resolution for SaveChangesAsync
I have two methods in my EF context class, one is an override and one is a new member like this:
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
......
7
votes
2
answers
154
views
GCC14 performes multiple implicit conversions instead of one matching explicit conversion
#include <cstdio>
#include <string>
class A {
std::string data;
public:
A() = default;
explicit A (const char* data) : data(data) {}
operator const char* () const;
...
2
votes
1
answer
122
views
Overload resolution between ordinary and explicit object member functions
In the following test program, struct B has two member functions f, which can be called using B{}.f(): one ordinary f() and another with explicit object f(this A).
struct A {
int f() { return 1; }
...
2
votes
1
answer
105
views
unexpected results in selecting conversion operator overload? [duplicate]
This question is based on previous SO discussion (which was affected by non-compliant compilers). So I'm using the latest c++23 released versions of gcc/clang/MSVC.
Here is a simple test with ...
3
votes
1
answer
2k
views
Understanding `impl dyn Trait`
I can't wrap my head around the second impl block. In my understanding, impl is typically used to implement a trait/methods on a concrete type like a struct. However, what does it mean to implement ...
6
votes
3
answers
317
views
Overloading and volatile
How is this expected to work?
struct S {};
void foo(volatile S&);
void foo(S);
int main() {
volatile S v;
foo(v);
}
Compilers disagree on it: MSVC accepts the code, while Clang and GCC ...
2
votes
2
answers
120
views
Parameter pack and perfect forwarding
I just wrote following simple code but it doesnt compile:
#include <iostream>
#include <string>
class Obj{
public:
std::string name = "Name";
std::string l_name = "...
0
votes
2
answers
92
views
Can the semantics of a translation unit depend on a function declaration that is never used?
Can the semantics of a well-formed C++ translation unit depend on the
presence of a function or function template declaration that is never
used? By "used", I mean it is selected by ...
8
votes
3
answers
491
views
In C++ why can't I take the address of a function that also has a templated version?
Here is a piece of code that tries to provide a pointer to an overloaded function to another function that can accept a value of any type:
template <typename T>
void AcceptAnything(T&&);
...
2
votes
1
answer
134
views
Compiler diverge on overload resolution using argument-dependent lookup and constraint expression
In the following program template function f calls overloaded template function p and one of the overloads is declared after f, but as far as I understand it must be found at the point of ...
30
votes
1
answer
2k
views
Overload resolution and template argument deduction - why is 0 special?
In the following example, 0 behaves in a special way: it chooses a different overload than one would expect for one example function call. I would like to know why. My understanding is also below.
#...
1
vote
1
answer
114
views
How does overload resolution work with variadic template arguments and a non template argument derived type
I'm trying to create a overload for a function with variadic template arguments for a type and all its possible derivates.
Naively i tried with just one overload and the base class as the first ...
2
votes
1
answer
76
views
Function template overload with std::function type parameter, nullptr value and shared_ptr type
In the example below, why does the last one calls the function overload with std::function as parameter?
#include <iostream>
#include <functional>
#include <memory>
template <...