-1

I see a lot of confusing language used here obviously you can use both the class and struct keywords to create user defined types but can the class and struct keywords themselves be viewed as types?

Geoff
  • 457
  • 3
  • 9
  • 1
    Possible duplicate of [C/C++ Struct vs Class](http://stackoverflow.com/questions/2750270/c-c-struct-vs-class) – Rama Jan 20 '17 at 15:08
  • 6
    @Rama I don't think so. – George Jan 20 '17 at 15:08
  • No, keywords are not types. Do you think `include` is a type? – SergeyA Jan 20 '17 at 15:11
  • 4
    @SergeyA For what it's worth, `include` is not a keyword either ;) – Quentin Jan 20 '17 at 15:12
  • 1
    @Quentin, agreed. Didn't mean to say it's a keyword, just wanted to find the really extravagant example. – SergeyA Jan 20 '17 at 15:14
  • 1. Skim through http://en.cppreference.com/w/ to get general concept of all relevant topics. 2. consult http://en.cppreference.com/w/ first whenever in doubt. 3. Read http://en.cppreference.com/w/ thoroughly to get deep understanding of what you're doing. 4. if none of that helps or you find an interesting insightful question - come here – szpanczyk Jan 20 '17 at 15:16
  • I would be interested in knowing more about what led you to ask this question, and what you mean by it, OP. Especially if you're thinking in terms of comparisons to other languages. – Kyle Strand Jan 20 '17 at 15:23
  • @KyleStrand Just some strange language my lecturer was using and some odd language in some question criteria "Describe the format of a struct and the use of an array of type struct". The question really has no relation to any other language from my point of view. – Geoff Jan 20 '17 at 15:28
  • @Corey That information would be helpful in the question itself. – Kyle Strand Jan 20 '17 at 15:32
  • Also, was your lecturer talking about C or C++? That phrasing surprises me somewhat for talking about C++. – Kyle Strand Jan 20 '17 at 16:37
  • @KyleStrand Both the lecturer and question criteria were talking about C++, only the lecturer mentioned `class` s and the criteria mentioned `struct` s. – Geoff Jan 20 '17 at 17:29
  • Well, an "array of type struct" sounds like a declaration of the form `struct MyType my_array[num_elements];`. This does indeed look syntactically like an "array of type `struct`, since it's an array declaration and the first token is `struct`. But in C++ you don't need to specify `struct` once `MyType` has been declared. – Kyle Strand Jan 20 '17 at 19:42
  • Personally, I don't even understand what your professor means by "format of a struct", unless he wanted you to talk about what the runtime data structure created from a `struct` definition is like. (I.e., a block of memory containing the various data members from the definition, plus any inherited object instances, plus optional padding, plus sometimes a v-table pointer). – Kyle Strand Jan 20 '17 at 19:48
  • So if it were me, I'd edit your question to ask specifically whether the phrase "array of type `struct`" is meaningful and whether it implies that `struct` on its own is a type. This would clarify your question and make it eligilbe to reopen. – Kyle Strand Jan 20 '17 at 19:48

1 Answers1

3

The keyword, no they don't. Is like asking what is the type of if. You see, type is a property of an expression. if is not an expression. It therefore does not make sense asking for his type. The same goes with struct and class

However, types can be treated as value. When doing template meta programming, types become values. Here's some example:

template<int v>
struct constant { 
    constexpr static auto value = v;
};

template<typename A, typename B>
struct addition {
    constexpr static auto value = A::value + B::value;
};

int main() {
    // Prints 7
    std::cout << addition<constant<2>, constant<5>>::value << std::endl;
}

Even though struct has no type, it may has other properties. According to p0194r0, you can reflect a keyword.

using meta_struct = reflexpr(struct);

/* do whatever you want with meta_struct:: */

This proposal is headed for a TS and has a basic implementation in clang.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141