FAQ Database Discussion Community


Overloading template by Return Type

c++,templates,overloading,return-type,specialization
Mooing Duck makes a comment here that "One function can't return multiple types. However, you can specialize or delegate to overloads, which works fine." I started thinking about that, and I'm trying to figure out, how is this legal code: template <typename T> T initialize(){ return T(13); } When called...

How to return dynamic object from operator function?

c++,operator-overloading,dynamic-memory-allocation,return-type
I am quite confused about this. How to return a dynamically allocated object from operator function? Consider following example: #include "stdafx.h" #include <iostream> #include "vld.h" using std::cout; class Point { public: Point(int x,int y) : a(x),b(y) { } Point() { } Point operator + (Point p) { Point* temp=new Point();...

Returned uint64_t seems truncated

c,64bit,return-type,uint64
I would like to return a uint64_t but the result seems truncated: In lib.c: uint64_t function() { uint64_t timestamp = 1422028920000; return timestamp; } In main.c: uint64_t result = function(); printf("%llu = %llu\n", result, function()); The result: 394745024 = 394745024 At compilation, I get a warning: warning: format '%llu' expects...

Infinite loop seems to confuse Scala's type system

scala,infinite-loop,type-inference,return-type
Here is an artificial toy example that demonstrates my problem: def sscce(): Int = { val rand = new Random() var count = 0 while (true) { // type mismatch; found: Unit, required: Int count += 1 if (rand.nextInt() == 42) return count } } How can I help the...

C arrays and functions, how to return?

c,arrays,return-type
I have a newbie question regarding array and functions in C. Let's say I have this array: int array1[10] = {2,4,6,3,2,3,6,7,9,1}; I wrote this function: int *reverseArray(int *array, int size) { int *arr = malloc(size * sizeof(int)); int i, j; for(i = 10, j = 0; i > 0; i--,...

Function Overloading in AS3

actionscript-3,flash,return-value,return-type,function-overloading
I just read online that as3 does not support function overloading. I looked questions related to that here in the website but the answers don't seem clear enough to solve my problem. I tried overloading an event class the following way: public function SelectEvent(type:String, selecteditem:Buyer, bubbles:Boolean=false, cancelable:Boolean=false) { super(type,bubbles,cancelable); selectedbuyer...

is return main(); a valid syntax?

c,function,return,language-lawyer,return-type
I found some interesting code lines: #include <stdio.h> int main() { printf("Hi there"); return main(); } It compiles ok (VS2013) and ends up in stackoverflow error because of the recursive call to main(). I didn't know that the return statement accepts any parameter that can be evaluated to the expected...

pass string array values as return type string when condition is met

c#,arrays,string,function,return-type
I've a module which returns string array "string[]". It contains success code and author name. var get_author = SetBookInfo(Id, Name); this function SetBookInfo returns response code and author name. my condition is :: if response code is "sucess" return author name "william". ["success", "william"] if response code is "failed" return...

Why isn't my return statement being recognized?

java,return,return-type
It says it must return a string. But it already is. Then it says that it must have a return statement. But there is one there. public String description() { String description = ""; if (description != null) return description; } ...

Return type of list front (C++)

c++,function,reference,linked-list,return-type
So I want to use a list for a part of my program. I'm trying to get acquainted to the library list, so I wrote a quick little program to help myself understand what's going on. It all works properly, but there's one thing I don't understand. According to this:...

control reaches end of non-void function [-Werror=return-type]

c++,if-statement,return-type
Even after using default else statement I am getting this error int max_of_four(int a, int b, int c, int d) { if((a>b)&&(a>c)) { if(a>d) return a; } else if((b>c)&&(b>d)) { return b; } else if(c>d) { return c; } else return d; } ...

How do I declare multiple variables instantiated by a function call returning multiple values in Go?

syntax,go,return-type,variable-declaration
Let's say I have a function: func foo() (bool, string) { ... } And then I wish to declare two variables b and s, initialized with values returned by the function call foo(). I'm aware I can do this using the "shorthand" syntax which omits type annotations: b, s :=...

Return type of a constructor in c++

c++,constructor,return,return-type
I am learning c++, and I came across following code segment: class X { int i; public: X(int ii = 0); void modify(); }; X::X(int ii) { i = ii; } void X::modify() { i++; } X f5() { return X(); } const X f6() { return X(); } void...

Why can't I have return types covariant with void*?

c++,covariance,return-type
Why is the following code a covariance error? Isn't T * covariant with void *...? struct Base { virtual void *foo(); }; struct Derived : Base { int *foo(); }; GCC says: invalid covariant return type for 'virtual int* Derived::foo()' ...

When should I return pointer to object (not an object) from the operator function?

c++,operator-overloading,return-type
This is a follow up question to this. I understood that it returning pointer to object can result memory leak, also because of raw pointers they are error prone, also tricky. So,number of peoples advised me to not return pointer to an object from operator function. So,I am convinced to...

Register return Type

c#,generics,return-type
We have a C# class that holds session values for user in an MVC web application. Now I want to make the class more generic. Until now we have getters and setters for the session like public class WebAppLogin { public static WebAppLogin Current { get; //Gets the current Login...

I need the method to return all values from the select query

java,mysql,return-type
I've been working on a school project, I'm a student and a rookie at that, so if my question seems too basic or silly for that matter, please bare with me. That said, I've been working on a project that requires me to retrieve data from a mySQL database. The...

Return an anonymous instance/value in functions in C++

c++,anonymous-types,return-type
I have questions about the return type of function in C++. Why return pair<int, int>(1, 1); and return vector<int>(3, 3); work? Shouldn't I create an local instance var before hand and take it as the return value, just like the return a, because return int 1; doesn't work. // ......

Wildcard for Set in Map - return type incompatible - Why? [duplicate]

java,generics,dictionary,wildcard,return-type
This question already has an answer here: Java Generic List<List<? extends Number>> 5 answers The following is an example code to set up my problem situation: Foo interface: interface Foo<K> { Map<K, Set<? extends Foo<K>>> map(); Set<? extends Foo<K>> set(); } Bar interface: interface Bar<K> extends Foo<K> {} FooBar...

Mallocing Space for String Array in C

c,arrays,pointers,return-type,c-strings
I need help malloc space for a string array and then returning this value as a pointer. How do I go about doing this? My code is: char * strArray[*arrLen] = malloc(srcStrLen - numOfDelimsInStr + (numOfDelimsInStr + 1)); I populate string array with memcpy and manual '\0' adds. Then I...