0

The Caffe2 framework includes the following code in the file caffe2/core/operator.h:

  template <typename T>
  inline bool InputIsType(int idx) {
     return inputs_.at(idx)->template IsType<T>();
  }

I do not understand the third line of this code snippet: why is the template statement used here? To my knowledge, template is always followed by <> and used to define a type T, like in the first line of this code snippet.

Why do we need to write template before calling IsType<T>()?

hbaderts
  • 14,136
  • 4
  • 41
  • 48
  • Do I understand the question correctly: you ask what the part `template IsType()` means? – hbaderts May 05 '17 at 10:55
  • @hbaderts yes, what the part template IsType() means – user3132300 May 06 '17 at 11:42
  • does my answer clarify your question? As you see [here](http://stackoverflow.com/help/someone-answers), you can accept the answer if you believe this is the solution to your problem. Otherwise, please leave a comment on what is missing. – hbaderts May 09 '17 at 06:29

1 Answers1

0

The InputIsType method is part of the OperatorBase class, which is the base class of all operators in Caffe2. Each operator contains (among others) the following private fields (defined in caffe2/core/operator.h):

vector<const Blob*> inputs_;
vector<Blob*> outputs_;

These are the inputs and outputs of your operator. As an operator can have multiple inputs and/or multiple outputs, both are vectors.

The InputIsType method takes an input int idx, which defines which input Blob to look at (i.e. InputIsType(0) is the zero'th input Blob, and so on) and compares the type of that specific Blob to the type <T>. This is done using the IsType method of the Blob class, which is defined in caffe2/core/blob.h:

/**
 * Checks if the content stored in the blob is of type T.
 */
template <class T>
bool IsType() const { return meta_.Match<T>(); }

Note: This matches the type T to the type of our Blob. I won't go into details on how that works. Let's just assume this returns True or False as required.

So, to check if a Blob A has type int, we would have to call

A->IsType<int>();

However, as described in this answer, the compiler doesn't know whether you want to use the template with <int> here, or whether you are doing a comparison with the < (less than) sign. So we must tell the compiler we are using templates here, and call

A->template IsType<int>();

The same applies in the case of the InputIsType method: instead of using int in this example, we use the type T (which we defined before), and call IsType on element idx of the vector inputs_:

inputs_.at(idx)->template IsType<T>();
Community
  • 1
  • 1
hbaderts
  • 14,136
  • 4
  • 41
  • 48
  • could you give an example to show A->IsType (), < is less than, > is bigger than? I understand the case boost::function f; boost::function is a number. i do not understand A->IsType is a number less than T which is maybe also a number. A->IsType < T maybe return true and then true > () is nonsense. i think parser can't parse T as a number. – user3132300 May 09 '17 at 12:59
  • The *less than* and *bigger than* example is just an example how there can be ambiguity. In this case it is not exactly the same, but the idea is the same: you need to write `template` to explicitly tell the compiler that you want to call `IsType` for the type `T`. – hbaderts May 10 '17 at 08:33