overload-resolution Questions

1

Solved

Consider the following setup: struct MyInt { template<class T> operator T() = delete; operator int(){ return 42; } }; struct TestCArr { int arr[2]; }; struct TestStdArr { std::array&l...

2

Solved

#include <cstdio> #include <string> class A { std::string data; public: A() = default; explicit A (const char* data) : data(data) {} operator const char* () const; explicit o...

3

Solved

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 say...

3

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&&amp...

1

Solved

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

Solved

I've defined two versions of a function template called compare: #include <cstring> using namespace std; // fist version template <size_t N, size_t M> int compare(const char (&a)[...
Angell asked 22/12, 2023 at 3:19

1

Solved

The following code prints nullptr instead of empty (godbolt link): #include <iostream> class empty { }; #if 1 void f(std::nullptr_t) { std::cout << "nullptr\n"; } #endif v...
Neocolonialism asked 20/12, 2023 at 16:31

1

Solved

I have the following small C++ code sample, but I can't figure out why the compiler works this way, although I have spent quite a lot of time studying cppreference. I would appreciate any explanati...

4

Solved

Note: This is very similar to Determine number of bits in integral type at compile time, however this is a much simplified version, all contained in a single .cpp The problem is with functions like...
Anikaanil asked 14/5, 2012 at 8:7

1

Solved

Consider the following code: void foo(int* = 0); // (1) void foo(int = 0); // (2) int main() { foo(0); // OK, calls (2) foo(); // error, ambiguous overload } Both GCC and clang act like this, b...

1

Solved

Consider the following example. There are two function templates on lines marked (1) and (2) which get called on line (3). Both templates match the call, and it seems that none of them is more spec...
Helen asked 4/9, 2023 at 20:8

1

Solved

Can you guess the output of this trivial program? #include <vector> #include <string> #include <exception> #include <iostream> int main() { try { struct X { explicit X(...
Happy asked 30/8, 2023 at 21:46

2

Solved

I have a simple factory pattern where the implementation is determined through overload resolution. Problem is that the Kotlin compiler complains with "Overload resolution ambiguity.." for the inli...
Bespoke asked 1/3, 2016 at 20:11

3

Solved

#include <iostream> #include <string> struct mystruct{ mystruct(std::string s){ std::cout<<__FUNCTION__ <<" String "<<s; } explicit mystruct(bool s)...

1

Solved

What are the rules for the selection of overloaded function templates in case of non-type template parameters, if one of the parameters is a placeholder type. I am confused with the current behavio...

4

Solved

The following code does not compile: #include <iostream> #include <utility> struct Foo { Foo() { std::cout << "Foo()" << std::endl; } Foo(int) { std::cout << "Foo(...

2

Solved

I have a template for a function that accepts at least one parameter and performs some formatting on the rest: template <typename T, typename... ARGS> void foo(T first, ARGS&&... args...

1

Solved

[over.ics.rank]/4: [..] (4.3) If class B is derived directly or indirectly from class A, conversion of B* to A* is better than conversion of B* to void*, and conversion of A* to void* is better t...
Kory asked 6/7, 2022 at 14:11

1

Solved

I've got a test program to see how compiler(g++) match template function: #include<stdio.h> template<class T>void f(T){printf("T\n");} template<class T>void f(T*){printf...
Lucilius asked 27/6, 2022 at 23:59

3

Solved

In this link : Implicit object parameter In this quote : If any candidate function is a member function (static or non-static) that does not have an explicit object parameter (since C++23), but n...

2

Solved

Let's create currying function. template <typename TFunc, typename TArg> class CurryT { public: CurryT(const TFunc &func, const TArg &arg) : func(func), arg(arg ) {} template &lt...
Chane asked 9/5, 2022 at 13:53

1

In the following program, struct B has two user-defined constructors: one from int and another from int&& (clearly this is not very practical). And an object of B is created from an l-value...
Pester asked 6/2, 2022 at 8:47

2

Solved

I wrote those two overloads: int func(int, int) { return 1; } int func(double, double) { return 2; } When I call them with the obvious two calling schemes, i.e. func(1, 1) and func(1.0, 1.0)...
Glasgow asked 2/6, 2020 at 19:26

1

Solved

The following code fails to compile (Godbolt link): #include <concepts> template <class Fn> decltype(auto) g(Fn&& fn) { return fn(); } template <typename T> requires(std...

1

Solved

This code sample fails to compile due to ambiguous overload resolution void g(char (&t)[4]) {} void g(char *t) {} int main() { char a[] = "123"; g(a); } and careful reading of ove...
Kallick asked 30/3, 2022 at 5:31

© 2022 - 2024 — McMap. All rights reserved.