Skip to main content
Filter by
Sorted by
Tagged with
1 vote
1 answer
136 views

(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-...
La Creatura's user avatar
16 votes
1 answer
710 views

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 ...
Remy Lebeau's user avatar
1 vote
1 answer
143 views

Consider the following example: #include <iostream> #include <vector> template<typename T> void foo( T && ) { std::cout << "Common foo!\n"; } template&...
Kaiyakha's user avatar
  • 2,075
0 votes
0 answers
60 views

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 ...
v.oddou's user avatar
  • 6,867
6 votes
3 answers
160 views

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 ...
Igor G's user avatar
  • 2,668
6 votes
1 answer
164 views

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 ...
Oersted's user avatar
  • 3,904
5 votes
1 answer
206 views

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 <...
Fedor's user avatar
  • 24.8k
2 votes
1 answer
126 views

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 ...
John Grzegorczyk's user avatar
4 votes
2 answers
103 views

Why is method(new ExtendsRaw()) ambiguous in the code below? public class Main { private Main() {} static class Generic<T> {} @SuppressWarnings("rawtypes") static ...
Tomer Aberbach's user avatar
1 vote
0 answers
73 views

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<...
Andrew Carter's user avatar
1 vote
1 answer
73 views

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 ...
Joe's user avatar
  • 7,194
3 votes
1 answer
157 views

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&...
Ship's user avatar
  • 31
4 votes
1 answer
79 views

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 ...
glades's user avatar
  • 5,392
3 votes
2 answers
143 views

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 ...
codymanix's user avatar
  • 29.7k
6 votes
1 answer
107 views

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(...
geza's user avatar
  • 30.5k
0 votes
0 answers
78 views

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;...
JWWalker's user avatar
  • 22.8k
0 votes
1 answer
96 views

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&...
san216's user avatar
  • 95
5 votes
0 answers
118 views

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 ...
golvok's user avatar
  • 2,194
14 votes
3 answers
1k views

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 ...
michael3.14's user avatar
2 votes
1 answer
64 views

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 ...
Markus W.'s user avatar
  • 392
0 votes
1 answer
146 views

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, ...
user3188445's user avatar
  • 5,006
8 votes
1 answer
202 views

Consider the following setup: struct MyInt { template<class T> operator T() = delete; operator int(){ return 42; } }; struct TestCArr { int arr[2]; }; struct TestStdArr { ...
Velocirobtor's user avatar
1 vote
1 answer
84 views

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; ...
Ferenc Janky's user avatar
3 votes
1 answer
56 views

Consider the following code: #include <tuple> #include <type_traits> #include <cstddef> struct A { using type = std::tuple<int, float>; }; struct B : A { using type = ...
Igor R.'s user avatar
  • 15.1k
2 votes
0 answers
70 views

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) ......
Tanveer Badar's user avatar
2 votes
1 answer
86 views

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) { ...
Yuqi Huang's user avatar
7 votes
2 answers
159 views

#include <cstdio> #include <string> class A { std::string data; public: A() = default; explicit A (const char* data) : data(data) {} operator const char* () const; ...
Martin Horský's user avatar
1 vote
1 answer
73 views

#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....
iouvxz's user avatar
  • 175
2 votes
1 answer
125 views

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; } ...
Fedor's user avatar
  • 24.8k
2 votes
6 answers
212 views

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 ...
ashpool's user avatar
  • 255
2 votes
1 answer
106 views

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 ...
Gene's user avatar
  • 754
0 votes
2 answers
106 views

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 ...
Dorian's user avatar
  • 632
4 votes
1 answer
2k views

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 ...
Code learner's user avatar
0 votes
1 answer
83 views

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 { ...
Leon's user avatar
  • 2,165
6 votes
3 answers
321 views

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 ...
Dr. Gut's user avatar
  • 3,391
2 votes
1 answer
146 views

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 ...
BCS's user avatar
  • 79.2k
2 votes
2 answers
127 views

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 = "...
Qwe Qwe's user avatar
  • 557
1 vote
1 answer
112 views

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 ...
DiKetarogg's user avatar
0 votes
2 answers
94 views

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 ...
Scott McPeak's user avatar
  • 13.8k
2 votes
1 answer
88 views

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 ...
Enlico's user avatar
  • 30.3k
8 votes
3 answers
499 views

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&&); ...
jacobsa's user avatar
  • 7,903
-1 votes
1 answer
70 views

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 ...
tearfur's user avatar
  • 119
2 votes
1 answer
146 views

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 ...
Fedor's user avatar
  • 24.8k
0 votes
1 answer
146 views

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, ...
shuhalo's user avatar
  • 6,562
30 votes
1 answer
2k views

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. #...
toxic's user avatar
  • 600
2 votes
1 answer
95 views

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 ...
haiter's user avatar
  • 23
1 vote
1 answer
120 views

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 ...
ridilculous's user avatar
1 vote
2 answers
96 views

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 ...
TCH's user avatar
  • 148
2 votes
1 answer
77 views

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 <...
auzn's user avatar
  • 705
2 votes
3 answers
135 views

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) -> ...
levzettelin's user avatar
  • 3,062

1
2 3 4 5
17