I ran into the following code that defines a function template in a class:
#include <cstdint> class foo { public: enum class magic_type : std::uint32_t { START = 0, BLUE = 0xFF000001, RED, }; struct header_t { uint32_t version; magic_type magic; }; template <typename T> static bool is_of_type(header_t *h) { return (h->magic == T::magic_type); } foo(uint32_t ver, foo::magic_type mag) { header.version = ver; header.magic = mag; } header_t header; }; I am finding the implementation of 'is_of_type` confusing. The code as is compiles, so syntactically must be correct. However, this method is not invoked from any other part of the program, so I am not sure what the intent of the function is (lack of documentation). I figured there could be two interpretation of the function:
Return true/false based on the magic type of an object and the specific enum type passed as the function template parameter.
E.g. An invocation of the method would be:
foo bar(1.2, foo::magic_type::BLUE); bool temp = bar.is_of_type<foo::magic_type::BLUE>(&(bar.header));However, in the above case, I am not really passing a type (as in an int, or char, etc). Right? The code does not compile.
Return true/false if the magic type is a valid enum.
In this case, I am assuming the function does not need to be templated, and could be re-written as:
static bool is_of_type(header_t *h) { return (h->magic == foo::magic_type); }E.g. of an invocation:
foo bar(1.2, foo::magic_type::BLUE); bool temp = bar.is_of_type(&(bar.header));Again, getting compile error. I tried using "typename", but my attempts were futile.
Can someone please help me with proper implementation of is_of_type for the above two cases and an invocation example.
new foo…instead offoo bar(1.2, foo::magic_type::BLUE)? Hopefully you’re not using such code, with redundantnews, in reality.