0

I'm reading the C++ code below:

// Take a whole packet from somewhere.
EthernetII packet = ...;
// Find the IP layer
const IP* ip = packet.find_pdu<IP>();
if(ip) {
    // If the pointer is not null, then it will point to the IP layer
}
// Find the TCP layer. This will throw a pdu_not_found exception
// if there is no TCP layer in this packet.
const TCP& tcp = packet.rfind_pdu<TCP>();

The definition of PDU::rfind_pdu() is below. My question is, should an argument not be passed to rfind_pdu()? And what is the syntax that allows <IP> to appended to the function call?

template<typename T> const T& Tins::PDU::rfind_pdu(PDUType type = T::pdu_flag) const inline

Finds and returns the first PDU that matches the given flag.

Parameters flag The flag which being searched.

nwp
  • 9,623
  • 5
  • 38
  • 68
netleap tom
  • 143
  • 4
  • 10
  • 3
    Looks like you want to read a bit from [here](https://stackoverflow.com/a/388282/3484570) until you have a good understanding of templates and default arguments. – nwp Jan 22 '18 at 16:01

1 Answers1

0

An argument need not necessarily passed, since it's defaulted to T::pdu_flag.

The <TCP> notation at the calling site disambiguates the template (that's required since the appropriate template instantiation cannot be gleaned from any function parameters since there aren't any). The default argument value will be TCP::pdu_flag, whatever that is.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483