0

I was attempting to template a C++ function but one of the templated structure's members isn't being recognized. My code is as follows:

template<typename  Tins, typename Tgload>                   
Tgload getreflection_terminated_twoport(const Tins &S, const Tgload &gammaload,bool loadport2)
{
    paramatrix S2p;         // S-parameters 2x2 matrix of the two-port
    if(typeid(Tins)==typeid(std::pair<double,paramatrix>))
    {
        S2p=S.second;
    }
    else if(typeid(Tins)==typeid(paramatrix))
    {
        S2p=S;
    }
    else
    {
        std::cout<<"ERROR in getreflection_terminated(): invalid type for 2-port S-parameters"<<std::endl;
        exit(10);
    }
}

I get a compiler error on the line containing S2p=S.second and the compiler complains that S has no member second. However one possibility of typename Tins Does have a member second as part of a pair. I would have thought that the compiler would have recognized, from the if(typeid(Tins)==typeid(std::pair<double,paramatrix>)) statement, that S could have a member second since std::pair<double,paramatrix>) does have a member second.

Why this doesn't work? and whether I can use such datatypes as template options?

I'm new to using templates and I've not been able to find an answer to this.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Jason Mar 24 '23 at 03:59
  • You want `if constexpr`. See [Difference between "if constexpr()" Vs "if()"](/questions/43434491/difference-between-if-constexpr-vs-if) – Jason Mar 24 '23 at 04:00
  • 1
    Thanks so much Jason for your quick response! I think this answers it and helps me move forward with my learning. The solution apparently is to use: ` if constexpr(std::is_same>::value) – montanaviking Mar 24 '23 at 04:39

0 Answers0