views:

362

answers:

5

If, for example, you're going to write a variant type class, you will naturally need identification of what type an instance of that class is carrying. I'm wondering if anyone knows of any official or semi-official (de-facto?) reference of all primitive datatypes one would possibly be interested in?

Only primitives, and no need for abstract types like string or handle.

Thanks.

A: 

Google Search is always a good start. Here. Now, roll out your actual question.

dirkgently
Yes direct everybody to Google Search instead of asking questions on StackOVerflow. Maybe you missed the whole idea of why we want SO and not Google Search, Google Groups or anything like that. Anyway, your link's not that good anyway, can't find longlong or int64 , so I don't trust the list. -1
sharkin
longlong and int64 are not defined in C++. -1 yourself
jalf
They are compiler/architecture specific types. You're talking about the C++ ISO standard which I haven't even mentioned.
sharkin
Perhaps your question should MENTION which types you're interested in then? Because there is a C++ tag in your question, which I take to mean "the context of my question is C++", but apparently, it meant "I don't actually want C++ types, but the proprietary ones that my unspecified compiler defines"
jalf
I'm interested in ALL types. Even longlong and int64. Many compilers today support them.
sharkin
Is that all types accepted by YOUR C++ compiler? All types accepted by SOME C++ compiler? OR all types accepted by A compiler, C++ or otherwise? (Might want to add this information to your question)
jalf
I acknowledge your point. Now let's quit this meaningless exchange of comments.
sharkin
So... are you going to update your question so we have a chance of answering it? ;)
jalf
I've nominated it for closure, you might like to join in.
sharkin
A: 

Use any third-party variant.

All data types you cant find in standard.

bb
+2  A: 

The only official reference is the ISO/IEC 14882 C++ Standard.

anon
+4  A: 

Have you considered letting another library do the heavy lifting?

There's Boost.Variant that probably does what you need, fully tested, typesafe and correct, and pretty efficient.

Or if you want to roll your own, use Boost.TypeTraits

jalf
Thanks for the tip, but this is a learning experiment.
sharkin
A: 

You don't need to know anything about types if you use typeid:

#include <typeinfo>
#include <iostream>

using namespace std;

struct var_base
{
 const type_info & t;
 var_base(const type_info & t) : t(t) {};

 virtual ~var_base() {};
};

template<class T> struct var : var_base
{
 T value;

 var(T x) : var_base(typeid(T)), value(x) {};
};

struct variant {
 const static int max_size=16;

 char data[max_size];
 var_base & v;

 variant() : v(*(var_base*)data) {
  new (data) var<int>(0);
 }

 const type_info & getType() { return v.t; }

 template<class T> T & get() {
  assert(getType()==typeid(T));
  return static_cast< var<T> &>(v).value;
 }

 template<class T> void set(const T & value) {
   // Compile time assert is also possible here.
  assert(sizeof(var<T>)<=max_size);
  v.~var_base();
  new (data) var<T>(value);
 }
};

main()
{
 variant v;
 v.set<float>(1.2);
 cout << v.getType().name() << endl;
 cout << v.get<float>();
 cout << v.get<int>(); // Assert fails
}

Note that you can get rid of max_size, if you can accept that the value is dynamically allocated. I just wanted to show that in place allocation works too, if you know the size of the largest type.

TrayMan