1

Topic: How to make a template, at which parameter - structure with potentially different fields?

There are two structures:

struct A
{
int a;
static std:: string Field () {return "a";}
};
struct B
{
int b;
static std:: string Field () {return "b";}
};

It is necessary to write sample function which can work simultaneously with two structures (in such variant swears that the structure And does not have field "b", and structure B does not have field "a"):

template <class Type> FillStruct (Type CurrentStruct)
{
if (Type:: Field () == "a") CurrentStruct.a = 42;
else (Type:: Field () == "b") CurrentStruct.b = 42;
}

In a reality at me the order of 10 structures, and at everyone on 5-6 fields, therefore to analyze "intersections" of fields at all does not please. To write ten same functions too it would not be desirable...
How flies up?

2

Re: How to make a template, at which parameter - structure with potentially different fields?

AlekseySQL wrote:

As flies up?

Throw out templates, structures inherit from one abstract with the virtual method FillStruct.

3

Re: How to make a template, at which parameter - structure with potentially different fields?

Dimitry Sibiryakov wrote:

Throw out templates, structures inherit from one abstract with the virtual method FillStruct.

And sense? After all all was started to write ONE method of data handling. And in your case it is necessary to write two  a method...
p.s. With coming New Year! smile

4

Re: How to make a template, at which parameter - structure with potentially different fields?

AlekseySQL wrote:

And sense? After all all was started to write ONE method of data handling.

Then do not show off and give to identical fields identical names.

5

Re: How to make a template, at which parameter - structure with potentially different fields?

Dimitry Sibiryakov wrote:

Then do not show off and give to identical fields identical names.

These are different fields smile I have a dial-up text csv - files which by means of the regular expressions are read out line by line, but in one case one fields, and in the friend - others are necessary. For example, in one case I find a total sum in a section of the goods, and in other - an amount of clients on the markets... All this field is "columns" of a csv-file which I read regular .
Very much it would not be desirable to write for each case similar functions of reading of a text line  expressions and structure fillings.

6

Re: How to make a template, at which parameter - structure with potentially different fields?

AlekseySQL wrote:

very much it would not be desirable to write for each case similar functions of reading of a text line  expressions and structure fillings.

Well then only union to you helps)

7

Re: How to make a template, at which parameter - structure with potentially different fields?

ptr128 wrote:

it is passed...
Well then only union to you helps)

Did not understand as sharing different variables of one storage helps with my task?

8

Re: How to make a template, at which parameter - structure with potentially different fields?

AlekseySQL;
Helps that in one variable of this kind can contain both structure And, and B.No's structure not both simultaneously.

9

Re: How to make a template, at which parameter - structure with potentially different fields?

AlekseySQL;
That that you want is implemented through SFINAE.
https://en.wikibooks.org/wiki/More_C _Idioms/Member_Detector
But most likely that you want, does not coincide that is necessary to you))
For example it is possible so:

struct A
{
int a;
int& Field () {return a;}
};
struct B
{
int b;
int& Field () {return b;}
};
template <class Type> void FillStruct (Type& CurrentStruct)
{
CurrentStruct. Field () = 42;
}
int main ()
{
A a;
FillStruct (a);
B b;
FillStruct (b);
return 0;
}

10

Re: How to make a template, at which parameter - structure with potentially different fields?

Anatoly Moskovsky;
Thanks, but are a lot of fields, in each structure the output agent of filling therefore decided to write.

11

Re: How to make a template, at which parameter - structure with potentially different fields?

struct A
{
int a;
};
struct B
{
int a;
};
template <class Type> FillStruct (Type& CurrentStruct)
{
CurrentStruct.a = 42;
}

Other variant to use is_same . At first, it  a code part at a compilation stage, secondly, will not be necessary superfluous determinants of type, type Field ().

12

Re: How to make a template, at which parameter - structure with potentially different fields?

AlekseySQL;
I. It is possible through specialization of a template for each structure (probably, not the most convenient decision);
II. It is possible through SFINAE - get enum class FieldType for each field type, get  a field in each structure, type static const FieldType fieldType = FieldType:: A; , and through std:: enable_if_t narrow down area for  templates for one/several values, type

template <class Type>
std:: enable_if_t <Type:: fieldType == FieldType:: A, void> FillStruct (Type CurrentStruct)
{
CurrentStruct.a = 42;
}
template <class Type>
std:: enable_if_t <Type:: fieldType == FieldType:: B, void> FillStruct (Type CurrentStruct)
{
CurrentStruct.b = 42;
}

That is in different structures it is possible to use the same field value FieldType. Remember that to values enum class' bit operations that it is possible to use as bit flags in conditions at disclosure of templates also are applicable. Alternatively
III. It is necessary to look also on if constexpr if you already have C ++ 17. In this case, just it would be possible to write as you wish

template <class Type> FillStruct (Type CurrentStruct)
{
if constexpr (Type:: fieldType == FieldType:: A) CurrentStruct.a = 42;
else if constexpr (Type:: fieldType == FieldType:: B) CurrentStruct.b = 42;
}

13

Re: How to make a template, at which parameter - structure with potentially different fields?

Thanks for a heap of sensible councils!

14

Re: How to make a template, at which parameter - structure with potentially different fields?

AlekseySQL wrote:

Thanks for a heap of sensible councils!

as a variant