LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-26-2007, 05:24 AM   #1
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
$#^!% template specialization headache!


Okay, I am mad. I am only mad because I've scoured the 'net, read all the books I own, and tried a million different combinations on my code. No avail. No one on the 'net has ever done this. None of the books think you can/should. And I certainly can't figure it out through combinations. So, even though I am certain I am missing something SSOOO simple, how do I do this:

Code:
/* Include nescessary headers */
#define ERROR CErrorHandler()
#define ERR_END CErrorHandler::end

class CErrorHandler
{
	public:
		typedef int end_type;
		static end_type end;
		
		template<typename T> CErrorHandler& operator<<(const T &Val)
		{
			Message << Val;
			return (*this);
		}
		
		template<CErrorHandler::end_type> CErrorHandler& operator<<(const CErrorHandler::end_type &Val)
		{
			Message << "\n";
			cout << "Error: " << this->str();
			throw Message;
		}
		
		string str()
		{
			return Message.str();
		}
		
	private:
		stringstream Message;
};

void main()
{
    ERROR << "This is an error message" << ERR_END;
}
The above code doesn't work. As you can see, I want to make an error handler that works like a stream, and as soon as end_type type is collected in the stream, it will actually THROW the error. However, this doesn't work. I've tried everything, if I do a:

Code:
template<typename T> CErrorHandler& operator<<(const T &Val)
		{
			Message << Val;
			return (*this);
		}
		
		template<> CErrorHandler& operator<< <CErrorHandler::end_type>(const CErrorHandler::end_type &Val)
		{
			Message << "\n";
			cout << "Error: " << this->str();
			throw Message;
		}
Then g++ gives the following errors (that, as usual, are extremely descriptive... right... NOT!):
Code:
In file included from source/main.cpp:5:
include/main.h:27: error: invalid explicit specialization before '>' token
include/main.h:27: error: explicit specialization in non-namespace scope `class CErrorHandler'
include/main.h:28: error: invalid member function declaration
make: *** [source/main.o] Error 1
 
Old 04-27-2007, 03:11 AM   #2
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Okay, I am a little cooler now, and I am sorry I exploded, but I still need help just as badly. Please help me if you can.
 
Old 04-27-2007, 04:39 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
If I wanted to do something like this, then I would code it like the following
Code:
#include <iostream>
#include <sstream>
#include <stdexcept>

#define ERROR CErrorHandler()
#define ERR_END CErrorHandler::end

class CErrorHandler
{
public:
	struct end_type{};
	static end_type end;

	template<typename T> CErrorHandler& operator<<(const T &Val)
	{
		Message << Val;
		return (*this);
	}

	void operator<<(const CErrorHandler::end_type &Val)
	{
		Message << std::endl;
		std::cout << "Error: " << str();
		throw std::runtime_error(Message.str() );
	}

	std::string str()
	{
		return Message.str();
	}

private:
	std::stringstream Message;
};
CErrorHandler::end_type CErrorHandler::end;

int main()
{
	ERROR << "This is an error message" << ERR_END;
}
 
Old 04-27-2007, 03:45 PM   #4
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Thank you! I didn't know that was possible... see, you learn something new everyday!

I appreciate the help. I got around my problem by just making "end" a function. Since it threw a exception, it never returned, and that means the return value didn't matter anyhow, plus the return value would never get put in the error stream.

What you posted is allot of help, thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Suitable Specialization course for Msc Computer Science student in India anoopkjm Linux - Certification 2 11-25-2006 07:31 AM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM
template class ckcheung0927 Programming 3 11-28-2004 03:59 PM
c++ template help ashirazi Programming 4 11-16-2004 08:52 AM
Explicit specialization in non namespace scope ashwinipahuja Programming 3 06-04-2004 07:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration