Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 21 Dec 2004
Offline Last Active Feb 09 2011 05:24 AM

Topics I've Started

C++ Templates

05 June 2010 - 03:31 AM

I was under the impression that templates in C++ were actually handled at compile time to generate classes as necessary, and yet I've managed to come across something that doesn't work...

class Works {
std::map<std::string,int> testMap;

//this doesn't compile, as it shouldn't, since it does nothing

//compiles fine
typedef std::map<std::string,int>::iterator TestIterator;

template<typename T>
class Broken {
std::map<std::string,T> testMap;

//this compiles, but is completely useless

//doesn't compile
typedef std::map<std::string,T>::iterator TestIterator;

So here's my question: is it simply due to the fact that you can't use templates within templated classes (and if so, why do vectors work fine?), or is there something wrong with my understanding of templates, or is there something wrong with GCC?

Feeling like you need to be somewhere else...

16 January 2006 - 04:56 AM

Almost a year ago, I felt like I had to be somewhere else, and it was extreemly important. I knew I had no reason to be anywhere else, and did nothing about it, but ever since, I've felt like I'm missing something... I'm just wondering how many of you have felt like you needed to be somewhere else, and if you did, if you actually tried to find that place. That and I'm a little bored, so reading your replies should keep me entertained for a little while. [EDIT] Replaced 'it' with 'that place', since it sounds better.

Constant can't appear in a constant expression

01 June 2005 - 04:51 AM

Don't bother reading this if you aren't bored out of your mind (I wont even care if nobody replies). I got bored, and decided to make something to read avis (WITHOUT VFW). It works as good as I had expected (wont read compressed data, and I don't plan on making it do so), however, I did come across a small problem when I tried to rewrite the MAKEFOURCC define macro to accept strings (to increase readability and save time), and I was wondering if anybody knew why.
//the new define macro, and an example of how it works
#define CreateRCode(name,str)   const unsigned int name = (str[0] | (str[1] << 8) | (str[2] << 16) | (str[3] << 24))

//some code that belongs in a function:

//a variable

//some code that works
printf("N == RIFF?: %d\n",n == RIFF_RCODE_RIFF);

//the code that doesn't work
switch (n){
  case 0: printf("ZERO\n"); break;
  case RIFF_RCODE_RIFF: printf("RIFF\n"); break; //this line wont work

//a working alternative (like MAKEFOURCC, only cleaner):
#define CreateRCode2(name,a,b,c,d)   const unsigned int name = (a | (b << 8) | (c << 16) | (d << 24))

Incase you're wondering exactly what the compiler output is, it's this: `RIFF_RCODE_RIFF' cannot appear in a constant-expression. Also, I might as well mention that I'm using a combination of both CreateRCode and CreateRCode2, since I only use the switch statements when finding chunks that need to be discarded, but halting when there is garbage in the file, and I'd prefer not to have to use a giant if statement (since a giant switch statement is cleaner). ;)