802 questions
1
vote
1
answer
136
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-...
16
votes
1
answer
710
views
How does overload resolution select from multiple prospective destructors?
After seeing this post: If a class has a destructor declared with a requires clause that evaluates to false, why is the class not trivially destructible? I have a separate question, and I haven't ...
1
vote
1
answer
143
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&...
0
votes
0
answers
60
views
Pass a variadic templated function taking universal references as pointer to a utility
I have a problem with an attempt at factorizing two utility functions, each calling a given "hardcoded" function, into one utility with a functor call instead. Meaning I have a factoring ...
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 ...
6
votes
1
answer
164
views
function selection finding unexpected candidate when using namespaces
This is a follow-up of this question: `requires` expressions and function namespaces
Trying to design an answer I stumbled on this strange behavior:
#include <cstdio>
// dummy default ...
5
votes
1
answer
206
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 <...
2
votes
1
answer
126
views
C++ Templated Overload Resolution Fails Where Standard Resolution Wouldn't
There appears to be a discrepancy in how templated function overloads are resolved and how non-templated function overloads are resolved, but there seem to be some cases where it should not make a ...
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
0
answers
73
views
"Preloading" a partial set of args into a variadic function [duplicate]
Objective
I'm attempting to wrap a variadic function so I can provide an arbitrary set of arguments now and provide the rest later.
Ultimately, I want to be able to do something like this:
template<...
1
vote
1
answer
73
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 ...
3
votes
1
answer
157
views
C++ overloading resolution when passing initializer lists as arguments
can anyone explain why both cases print "plain int"?
#include <iostream>
void f(const int (&)[2])
{std::cout<<"int array"<<std::endl;}
void f(const int&...
4
votes
1
answer
79
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 ...
3
votes
2
answers
143
views
Simple string.Split failing with StringSplitOptions.TrimEntries
This returns an array with only one element and thus throws an exception. But it works when I omit StringSplitOptions.TrimEntries.
using System;
public class Program
{
public ...
6
votes
1
answer
107
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
0
answers
78
views
Why do 'long' and 'long long' types behave differently when both are 64 bits? [duplicate]
I'm compiling for the x86_64 architecture using Apple Clang (Xcode 14). In the following code:
#include <iostream>
#include <stdint.h>
static void foo( long v )
{
std::cout << v;...
0
votes
1
answer
96
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&...
5
votes
0
answers
118
views
Why is a requires clause not enough to disambiguate a function with dependent type?
In the following sample (try online), adding the trivial constraint requires true is enough to select the right overload when the argument is an int, but not enough when it's a dependent type. Both ...
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 ...
2
votes
1
answer
64
views
How does the mechanism behind overloading for member function arguments in a requires expression work?
This is a follow-up question to How to apply a concept to a member function in a concept and then use it?. My focus is not on how to fix this code but on understanding the underlying mechanism.
Assume ...
0
votes
1
answer
146
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, ...
8
votes
1
answer
202
views
Overload resolution for implicit conversion operators used in aggregate Initialization
Consider the following setup:
struct MyInt {
template<class T>
operator T() = delete;
operator int(){ return 42; }
};
struct TestCArr {
int arr[2];
};
struct TestStdArr {
...
1
vote
1
answer
84
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;
...
3
votes
1
answer
56
views
Counterintuitive template overload resolution
Consider the following code:
#include <tuple>
#include <type_traits>
#include <cstddef>
struct A {
using type = std::tuple<int, float>;
};
struct B : A {
using type = ...
2
votes
0
answers
70
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)
......
2
votes
1
answer
86
views
Template function overloading behavior changes with function declaration order
I have the following C++ code which outputs 12:
#include <iostream>
using namespace std;
template<typename T>
T foo(T val) {
return val;
}
template<typename T>
T bar(T n) {
...
7
votes
2
answers
159
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;
...
1
vote
1
answer
73
views
C++ overload resolution problem: How to make vector fill constructor overload instead of vector initializer list construtor?
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<char> v1 = { size_t(0),0 };
vector<char*> v2 = { size_t(0),0 };
cout << v1....
2
votes
1
answer
125
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
6
answers
212
views
C++ function template specialization based on the templated return type
In C++20, instead of doing
size_t count = 42;
std::vector<int> my_vector;
vec.reserve(count);
std::copy_n(std::istream_iterator<T>(std::cin), count, std::back_inserter(vec));
I want to be ...
2
votes
1
answer
106
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 ...
0
votes
2
answers
106
views
How to select the right overload of function template?
I'm using libcxx 16.0.0 from the LLVM project.
In __algorithm/fill_n.h, there is the following function:
// fill_n isn't specialized for std::memset, because the compiler already optimizes the loop to ...
4
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 ...
0
votes
1
answer
83
views
Specialize function template for all types of pointers
I want to implement a streaming style logging lib.
I made a Log_t class to buffer a log entry, and do real output up on it being
destroyed, like this:
class Log_t: public std::ostringstream {
...
6
votes
3
answers
321
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
1
answer
146
views
Why can't C++ overload resolution deduced nested template types?
This purported duplicate explains the mechanism of why this isn't allowed and shows a corner case where it can't work, but fails to address the question of why C++ refuses to allow it in the cases ...
2
votes
2
answers
127
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 = "...
1
vote
1
answer
112
views
Making an overload take effect only if no same overloads exist already
I want to make a simple ostream& operator << overload, but I don't want any ambiguity errors from the compiler if there so happens to be another overload.
Here is the closest thing I could ...
0
votes
2
answers
94
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 ...
2
votes
1
answer
88
views
What are the implications of over.over#note-2?
Yeah, just what does over.over#note-2 (which I casually stumbled on while reading this answer) mean? I really don't understand.
For context, I copy the text of over.over#2 below:
If there is no ...
8
votes
3
answers
499
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&&);
...
-1
votes
1
answer
70
views
C++ How to allow copy-list-initialization for a perfect forwarding function?
Suppose I have a perfect forwarding function:
template <typename T>
void f(T&& t) { /* does something */ }
I have some types T that I want to allow Copy-list-initialization for, say ...
2
votes
1
answer
146
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 ...
0
votes
1
answer
146
views
Ambiguity due to implicit conversion when using a template but not with normal function
I am trying to write a custom string class MyString with implicit conversion from const char*.
In addition to that, I want to output MyString via the shift operators into any std::ostream. Even more, ...
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.
#...
2
votes
1
answer
95
views
How to make operator= accept derivatives of parameter like operator+?
I don't understand why a = b doesn't print out value from operator= 5 like operator+ (which seems to allows derivatives). Why does it do this instead of allowing derivatives and how can I make it ...
1
vote
1
answer
120
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 ...
1
vote
2
answers
96
views
Priority confusion on function overloading with multiple parameters [duplicate]
I'm learning C++ at school. We are currently on the topic of overloading functions. We were given an exercice with a intial code block. Then, we are shown a function call, we have to determine which ...
2
votes
1
answer
77
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 <...
2
votes
3
answers
135
views
How can I get the behavior of a plain `auto` return type when using "expression SFINAE"?
Consider the following code.
struct Widget {
int& get();
};
template<typename X>
auto func_1(X& x) {
return x.get();
}
template<typename X>
auto func_2(X& x) -> ...