overload-resolution

problems with overload resolution and operator<< for templated types - part 2

↘锁芯ラ 提交于 2019-12-06 01:44:15
Given the following code: #include <string> #include <type_traits> #include <sstream> #include <vector> #include <iostream> using namespace std; namespace has_insertion_operator_impl { typedef char no; typedef char yes[2]; struct any_t { template <typename T> any_t(T const&); }; no operator<<(ostream const&, any_t const&); yes& test(ostream&); no test(no); template <typename T> struct has_insertion_operator { static ostream& s; static T const& t; static bool const value = sizeof(test(s << t)) == sizeof(yes); }; } template <typename T> struct has_insertion_operator : has_insertion_operator_impl

function call ambiguity with pointer, reference and constant reference parameter

喜你入骨 提交于 2019-12-05 21:01:39
What I am trying to do is, allow a pointer, reference or constant reference to be passed with the setter function: class A{ std::string * p; std::string st; public: A():p(0) {} A& setS(const std::string& s){ std::cout<<"called with const std::string&\n"; st = s; p = &st; return *this; } A& setS(std::string& s) { std::cout<<"called with std::string&\n"; p = &s; return *this; } A& setS(std::string* s) { std::cout<<"called with std::string*\n"; p = s; return *this; } }; int main(){ std::string s; A a; a.setS(std::move(s)) //const std::string& .setS("") //const std::string& .setS(s) //std::string&

Call a functor with a specific function from an overload set

余生长醉 提交于 2019-12-05 19:34:35
Context In mathematics-related context, I'd like to define functors working on <cmath> functions. For the purpose of this question, we will be using std::invoke as our functor. This is ill-formed (live demo) : std::invoke(std::sin, 0.0); (g++-8.1) error: no matching function for call to 'invoke(<unresolved overloaded function type>, double)' Indeed, std::sin is an overload set and the compiler lacks the type information to choose one of those functions. Question How could I name a specific function from an overload set? With what could we replace LEFT and RIGHT so that the following is well

Type deduction for non-viable function templates

喜欢而已 提交于 2019-12-05 18:32:56
In his answer to this question and the comment section, Johannes Schaub says there's a "match error" when trying to do template type deduction for a function template that requires more arguments than have been passed: template<class T> void foo(T, int); foo(42); // the template specialization foo<int>(int, int) is not viable In the context of the other question, what's relevant is whether or not type deduction for the function template succeeds (and substitution takes place): template<class T> struct has_no_nested_type {}; // I think you need some specialization for which the following class

Why does the compiler when using an overload in another assembly sometimes require you to also reference a subassembly?

人盡茶涼 提交于 2019-12-05 16:49:33
There are quite a few questions/answers about the compiler error mentionend below and how to resolve it, but the question here is asking about some insights why in this case this is required. Why does a project A which uses an overload of a method of another referenced project B, which uses an object of project C in one of it's overloaded signatures require, that you reference project C from project A, even if you never use the object from project C? I guess it must have to do with the resolving of which overload to use, but I'd like to understand the concept behind. Here's an example: Put

When does overload resolution of non-dependent name take place, in definition context or point of instantiation?

怎甘沉沦 提交于 2019-12-05 12:23:41
3.4 [basic.lookup]/p1 Overload resolution (13.3) takes place after name lookup has succeeded. void g(long); void g(int, int); template<class T> void f() { g(0); } void g(int, int = 0) {} int main(){ f<int>(); } gcc compiles succeed, clang faild. When does overload resolution of non-dependent name take place, in definition context or point of instantiation? Or both are right? In both context. [temp.res] 14.6\8 If a hypothetical instantiation of a template immediately following its definition would be ill-formed due to a construct that does not depend on a template parameter, the program is ill

Overloading, generic type inference and the 'params' keyword

我们两清 提交于 2019-12-05 11:44:17
I just noticed a strange behavior with overload resolution. Assume that I have the following method : public static void DoSomething<T>(IEnumerable<T> items) { // Whatever // For debugging Console.WriteLine("DoSomething<T>(IEnumerable<T> items)"); } Now, I know that this method will often be called with a small number of explicit arguments, so for convenience I add this overload : public static void DoSomething<T>(params T[] items) { // Whatever // For debugging Console.WriteLine("DoSomething<T>(params T[] items)"); } Now I try to call these methods : var items = new List<string> { "foo", "bar

Overloading of C++ templated functions

给你一囗甜甜゛ 提交于 2019-12-05 11:37:08
I would think that the following code should be working, but both g++ and clang++ return the exact same error (although Visual C++ 2012 doesn't). #include <iostream> #include <tuple> template <int N, typename T> struct A { }; template <typename Tuple> double result(const Tuple& t, const A<0, typename std::tuple_element<0, Tuple>::type>& a) { return 0; } template <typename Tuple> double result(const Tuple& t, const A<std::tuple_size<Tuple>::value-1, typename std::tuple_element<std::tuple_size<Tuple>::value-1,Tuple>::type>& a) { return 1; } template <typename Tuple, int N> double result(const

SFINAE not happening with std::underlying_type

你离开我真会死。 提交于 2019-12-05 11:26:54
问题 Below SFINAE code with variadic templates compiles nicely using clang 3.7.1, C++14: #include <array> #include <iostream> #include <vector> #include <cstdint> enum class Bar : uint8_t { ay, bee, see }; struct S { static void foo() {} // std::begin(h) is defined for h of type H template<typename H, typename... T> static typename std::enable_if<std::is_pointer<decltype(std::begin(std::declval<H>()))*>::value>::type foo(const H&, T&&... t) { std::cout << "container\n"; foo(std::forward<T>(t)...);

Generic Method Resolution

筅森魡賤 提交于 2019-12-05 11:23:22
Consider the following code: public class Tests { public void Test() { Assert.AreEqual("Int", DoSomething(1)); } public static string DoSomething<T>(T value) { return "Generic"; } public static string DoSomething(int value) { return "Int"; } } As expected, the non-generic DoSomething method will be invoked. Now consider the following modification: public class Tests { public void Test() { Assert.AreEqual("Int", DoSomething(1)); } public static string DoSomething<T>(T value) { return "Generic"; } public static string DoSomething<T>(int value) { return "Int"; } } The only thing I've changed is