overloading

C11 type-generic expressions - why not just add function overloading?

依然范特西╮ 提交于 2019-12-04 22:26:43
I was just reading the Wikipedia article on C11 , the new version of the C standard released in Dec 2011, and I saw that one of the added features was "type-generic expressions": Type-generic expressions using the _Generic keyword. For example, the following macro cbrt(x) translates to cbrtl(x) , cbrt(x) or cbrtf(x) depending on the type of x : #define cbrt(X) _Generic((X), long double: cbrtl, \ default: cbrt, \ float: cbrtf)(X) This looks pretty horrible to me - if they are going to change the language anyways, why not just add function overloading like in C++? Potatoswatter C has one

C++ member function overloading with & (ampersand)

孤街浪徒 提交于 2019-12-04 22:18:48
How to pick the correct error() member function? Do I need to cast somehow? using namespace std; struct test { int error(); void error(int x); int fun(); }; int main() { auto f1 = &test::error; // how to pick the correct function? auto f2 = &test::fun; // works } Do I need to cast somehow? Yes, you can use static_cast . static_cast may also be used to disambiguate function overloads by performing a function-to-pointer conversion to specific type, as in std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper)); So you can: auto f1 = static_cast<int(test::*)()>(&test:

chef rewind cookbook_file definition from a wrapper cookbook recipe

佐手、 提交于 2019-12-04 21:33:01
I am using an cookbook github.com opscode-cookbooks/openldap. I wrote an wrapper cookbook "lab_openldap" that includes "openldap::server" recipe. The server.rb recipe uses following clausule to upload the PEM file from cookbooks files/ssl/*.pem to server to the location node['openldap']['ssl_cert']. if node['openldap']['tls_enabled'] && node['openldap']['manage_ssl'] cookbook_file node['openldap']['ssl_cert'] do source "ssl/#{node['openldap']['server']}.pem" mode 00644 owner "root" group "root" end end The PEM is tried to be read from "openldap" cookbook file/ssl/#{node['openldap']['server']}

Error with C++ operator overloading

孤街醉人 提交于 2019-12-04 20:50:18
#include<iostream> using namespace std; class complex { double real; double image; public: complex(double r=0,double i=0) : real(r), image(i) { }; complex(const complex& c) : real(c.real), image(c.image) { }; ~complex(){}; double re() const { return real; }; double im() const{ return image; }; const complex& operator =(const complex&c) { real = c.real; image = c.image; return *this; }; const complex& operator +=(const complex&c) { real += c.real; image += c.image; return *this; }; const complex& operator -=(const complex&c) { real -= c.real; image -= c.image; return *this; }; const complex&

Checking whether a non-hardwired member function exists with SFINAE

半城伤御伤魂 提交于 2019-12-04 20:09:03
I want to create proxies for member functions and operators. They must have the same return type and parameters, and must be good for several classes, which are given as template parameters. Even if the class does not have the particular member function or operator, I want it to compile instead of failing with an error, essentially SFINAE. If X has a method f() and Y does not have any method named f , I need Proxy<X> to have an f() as well that calls X::f() , and I need Proxy<Y> to compile and instantiate without any problems. Extracting the return type from a known function is no longer a

Which constructor is called first while passing null in the class having overloaded constructor?

删除回忆录丶 提交于 2019-12-04 19:29:01
问题 Below is the java class having 3 overloaded constructors : public class Test { public Test(Object i){ System.out.println("Object invoked"); } public Test(String i){ System.out.println("String invoked"); } public Test(int k){ System.out.println("Integer invoked"); } public static void main(String[] args) throws Exception { Test t = new Test(null); } } If null value is passed while creating the new instance of class, which constructor will be invoked ? What is the reason ? 回答1: Java always

Scala ambiguous reference to overloaded definition with two implicit parameters

北城以北 提交于 2019-12-04 19:14:41
lazy val productService = BeanLookup[ProductDataService] object BeanLookup { def apply[T](implicit manifest: Manifest[T], context: ActorContext) = { val beanLookup = context.actorFor("/user/spring/beanLookup") Await.result( (beanLookup.ask(LookupBean(manifest.erasure))(timeout)).mapTo[T](manifest), timeout.duration) } def apply[T](implicit manifest: Manifest[T], system: ActorSystem) = { val beanLookup = system.actorFor("/user/spring/beanLookup") Await.result( (beanLookup.ask(LookupBean(manifest.erasure))(timeout)).mapTo[T](manifest), timeout.duration) } } scalac complains : scala: ambiguous

overload vs default parameters in c++ standard

拜拜、爱过 提交于 2019-12-04 18:52:56
问题 I was reading another question, and it got me thinking. Often the standard specifies functions which have default parameters in their descriptions. Does the standard allow writing these as overloads instead? For example, the standard says that std::basic_string::copy has the following declaration: size_type copy(Ch* p, size_type n, size_type pos = 0) const; Could a conforming implementation of the standard library implement this instead as two functions like this? size_type copy(Ch* p, size

Why doesn't this overloading/namespace/template-related C++ code compile?

断了今生、忘了曾经 提交于 2019-12-04 18:36:13
问题 Here is some C++ code: namespace A { int f(int x) { return 0; } int f(long x) { return 1; } template<class T> int g(T x) { return f(x); } } namespace B { struct C {}; } namespace A { int f(B::C x) { return 2; } } void h() { A::g(B::C()); } In namespace A, the code declares a few overloads of a function f, and a templated function g which calls f. Then we declare a new type in namespace B and overload f for the new type in namespace A . Compiling with g++ 4.2 gives order.cpp: In function ‘int

Methods overloading with value and reference parameter types

这一生的挚爱 提交于 2019-12-04 18:10:51
问题 I have the following code : class Calculator { public int Sum(int x, int y) { return x + y; } public int Sum(out int x, out int y) { x = y = 10; return x + y; } } class Program { static void Main(string[] args) { int x = 10, y = 20; Calculator calculator = new Calculator(); Console.WriteLine ( calculator.Sum ( x , y ) ); Console.WriteLine ( calculator.Sum ( out x , out y ) ); } } This code work well despite that methods signature are differenciated only be the out keyword. But the following