overload-resolution

F# and interface-implemented members

一曲冷凌霜 提交于 2019-11-30 08:41:19
I have a vexing error. type Animal = abstract member Name : string type Dog (name : string) = interface Animal with member this.Name : string = name let pluto = new Dog("Pluto") let name = pluto.Name The last line, specifically "Name" generates a compiler error saying that "the field, constructor or member 'Name' is not defined". The workaround I've used is to write let name = (pluto :> Animal).Name However this is very annoying and creates a lot of visual noise. Is there something one can do in F# to just be able to resolve Name without telling the compiler explicitly that Name is a derived

distance calculation error in c++ [closed]

谁说我不能喝 提交于 2019-11-30 07:43:52
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . #include <iostream> #include <cmath> #include <vector> using namespace std; int square(int a){ return a*a; } struct Point{ int x,y; }; int distance (const Point& a,const Point& b){ int k=(int) sqrt((float)((a.x-b

Why is a public const method not called when the non-const one is private?

泪湿孤枕 提交于 2019-11-30 02:32:35
Consider this code: struct A { void foo() const { std::cout << "const" << std::endl; } private: void foo() { std::cout << "non - const" << std::endl; } }; int main() { A a; a.foo(); } The compiler error is: error: 'void A::foo()' is private`. But when I delete the private one it just works. Why is the public const method not called when the non-const one is private? In other words, why does overload resolution come before access control? This is strange. Do you think it is consistent? My code works and then I add a method, and my working code does not compile at all. NathanOliver When you call

Does SFINAE apply to function bodies?

本秂侑毒 提交于 2019-11-29 15:20:35
I have the following sample code: class Serializable {}; class MyData : public Serializable {}; void GetData( Serializable& ) {} template<typename T> void GetData( T& data ) { std::istringstream s{"test"}; s >> data; } int main() { MyData d; GetData(d); } ( Live Sample ) Based on overload resolution rules, the non-template version should be preferred because the base class is of type Serializable . However, I expect SFINAE to kick in when there are errors in the template version when it is instantiated for overload resolution (because if the >> operator is not defined for a type, it should not

F# and interface-implemented members

和自甴很熟 提交于 2019-11-29 12:15:33
问题 I have a vexing error. type Animal = abstract member Name : string type Dog (name : string) = interface Animal with member this.Name : string = name let pluto = new Dog("Pluto") let name = pluto.Name The last line, specifically "Name" generates a compiler error saying that "the field, constructor or member 'Name' is not defined". The workaround I've used is to write let name = (pluto :> Animal).Name However this is very annoying and creates a lot of visual noise. Is there something one can do

SFINAE: checking the existence of a function breaks when the overload is moved to other namespaces

拜拜、爱过 提交于 2019-11-29 11:14:26
I want to check for the existence of a function in a specific namespace using SFINAE. I have found SFINAE to test a free function from another namespace which does the job, but there are some things I don't understand. Currently I have this working code, straight from the linked question: // switch to 0 to test the other case #define ENABLE_FOO_BAR 1 namespace foo { #if ENABLE_FOO_BAR int bar(); #endif } namespace detail_overload { template<typename... Args> void bar(Args&&...); } namespace detail { using namespace detail_overload; using namespace foo; template<typename T> decltype(bar()) test

Overload resolution of template functions

前提是你 提交于 2019-11-29 07:12:49
问题 Consider this code: #include <iostream> //Number1 template<typename T1, typename T2> auto max (T1 a, T2 b) { std::cout << "auto max(T1 a, T2 b)" <<std::endl; return b < a ? a : b; } //Number2 template<typename RT, typename T1, typename T2> RT max (T1 a, T2 b) { std::cout << "RT max(T1 a, T2 b)" << std::endl; return b < a ? a : b; } int main() { auto a = ::max(4, 7.2); //Select Number1 auto b = ::max<double>(4, 7.4); //Select Number2 auto c = ::max<int>(7, 4.); //Compile-time error overload

Why does the number of elements in a initializer list cause an ambiguous call error?

為{幸葍}努か 提交于 2019-11-29 05:45:08
问题 Why are the first two calls to doSomething OK by the compiler, but using two elements in the list causes an ambiguous call? #include <vector> #include <string> void doSomething(const std::vector<std::string>& data) {} void doSomething(const std::vector<int>& data) {} int main(int argc, char *argv[]) { doSomething({"hello"}); // OK doSomething({"hello", "stack", "overflow"}); // OK doSomething({"hello", "stack"}); // C2668 'doSomething': ambiguous call return 0; } 回答1: What is happening here

distance calculation error in c++ [closed]

不羁的心 提交于 2019-11-29 05:22:48
#include <iostream> #include <cmath> #include <vector> using namespace std; int square(int a){ return a*a; } struct Point{ int x,y; }; int distance (const Point& a,const Point& b){ int k=(int) sqrt((float)((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y))); return k; } int main(){ vector<Point>a(10); for (int i=0;i<10;i++){ cin>>a[i].x>>a[i].y; } int s=0; int s1; int k=0; for (int i=1;i<10;i++){ s+=square(distance(a[0],a[i])); } for (int i=1;i<10;i++){ s1=0; for (int j=0;j<10;j++){ s1+=square(distance(a[i],a[j])); if (s1<s) { s=s1; k=i;} } } cout<<k<<"Points are:"; cout<<a[k].x; cout<<a[k].y; return

Obtaining address locations of an overload method

余生颓废 提交于 2019-11-29 04:14:27
How do I get all the address locations for functions/procedures/methods that is overloaded? For example, Dialogs.MessageDlgPosHelp is overloaded having two different versions of it - one without a default button and one with. How would I obtain the address locations for the two functions? TLama Based on this thread and what Thomas Mueller pointed there, you might define types with the same signatures as methods whose addresses you want to obtain (for each overload). If you then declare the variables of those types and assign method pointers to them you will make sure that compiler chooses the