1

Topic: How to "deduce" returned type of function?

Is map which as a key stores a variable title, and in value - a variable and necessity to cause for it :

struct sData
{
std:: experimental:: any Any;
bool NeedDelete;
};
std:: map <std:: string, sData> Map;

It would be desirable to write NOT sample function which returns value on a key (a variable title):

auto GetValue (cstring& Key)-> decltype (Map.at (Key).Any.type ().name ())
{
std:: experimental:: any Any = Map.at (Key).Any;
if (Any.type () == typeid (std:: string))
{
return std:: experimental:: any_cast <std:: string> (Any);
}
else if (Any.type () == typeid (QObject *))
{
return std:: experimental:: any_cast <QObject *> (Any);
}
}

How such to make?

2

Re: How to "deduce" returned type of function?

In any way
/thread

3

Re: How to "deduce" returned type of function?

AlekseySQL;
You understand that the C ++ does not support different types of returned value in one not-sample function, only if they are not divided if constexpr'.

4

Re: How to "deduce" returned type of function?

AlekseySQL;
The type of field value Any becomes known only by program operation.
At compilation of type any is not present, therefore even with templates of anything it is impossible to deduce type of returned value.
At you 2 variants:
1) to specify type template argument
2) to return any

5

Re: How to "deduce" returned type of function?

Anatoly Moskovsky;
You advise it a microscope, and it still did not master a hammer

6

Re: How to "deduce" returned type of function?

Comparing typeid, a flag of necessity of a call .
All it   with misunderstanding of basic principles of language.

7

Re: How to "deduce" returned type of function?

Forgot to write that map at me it is filled up during construction and has a qualifier const. The amount of data types is restricted.

8

Re: How to "deduce" returned type of function?

AlekseySQL;
Generally, idea of heterogeneous containers in a C ++ not such and new. Tynts , for example.

9

Re: How to "deduce" returned type of function?

Anatoly Moskovsky wrote:

AlekseySQL;
The type of field value Any becomes known only by program operation.
At compilation of type any is not present, therefore even with templates of anything it is impossible to deduce type of returned value.
At you 2 variants:
1) to specify type template argument
2) to return any

At me it has been made by the first variant, but it appeared that by a call  object which contains above-stated a field map + a method of obtaining of value,  nested objects are not caused:

MyLib:: VariativeData:: ~VariativeData ()
{
using PAIR = struct std:: pair <string, sData>;
for (PAIR& pair: Map)
{
if (pair.second. NeedDelete)
{
pair.second. Any. ~ any ();//DOES NOT CAUSE  NESTED OBJECT!!!
}
}
}

Therefore there was a necessity to receive objects and to cause in them . It can be made saving in structure the additional field characterizing type (and it is stupid to sort out with the help if... else if...). But as already there is a function of obtaining of value from Any, it wanted to alter it to use in . Similar it does not turn out...

10

Re: How to "deduce" returned type of function?

AlekseySQL wrote:

Therefore there was a necessity to receive objects and to cause in them . It can be made saving in structure the additional field characterizing type (and it is stupid to sort out with the help if... else if...). But as already there is a function of obtaining of value from Any, it wanted to alter it to use in . Similar it does not turn out...

Che - - that I did not wake up yet: itself yesterday wrote a piece of determination of type without any additional fields in structure (which then decided to implement in a method of obtaining of value:

if (Any.type () == typeid (std:: string))
{
return std:: experimental:: any_cast <std:: string> (Any);
}
else if (Any.type () == typeid (QObject *))
{
return std:: experimental:: any_cast <QObject *> (Any);
}

11

Re: How to "deduce" returned type of function?

It is possible to write still Sample function which returns parameter through the entering link to return parameter. Then to cause it it is possible as Not sample function.

template <typename> F (const cstring& Key, T& value)
//and:
int iValue = 0;
float fValue = 0.f;
F ("int", iValue);
F ("float", fValue);

12

Re: How to "deduce" returned type of function?

CEMb;
, practically so. Hardly corrected.

template <class T> T F (const std::string& Key, T& value)
{
//.... compute; value = result
return value;
}

13

Re: How to "deduce" returned type of function?

Call

res = F ("varname", &res);

14

Re: How to "deduce" returned type of function?

AlekseySQL, what hinders to overload for sData operator of coercion of type?

struct sData
{
std:: experimental:: any Any;
bool NeedDelete;
operator std:: string ()
{
return std:: experimental:: any_cast <std:: string> (Any);
}
operator QObject* ()
{
return std:: experimental:: any_cast <QObject *> (Any);
}
};

15

Re: How to "deduce" returned type of function?

Siemargl;
Artfully! smile
It is possible to write expression to one line. Still of the second parameter to get rid... It can be played somehow with the operator "=" or the designer? In the same place

Type res = F ("varname", &res);

It generally:

Type res (F ("varname", &res));

As though here to contrive and to transfer in the designer type? It does not solve all "problems", but life simplifies.