   C++  the Troubles with Templates
  I was writing an XML producing program as an experiment in template meta-programming, and ran across a problem. There appears to be no way for me to place any form of string into a template. I have tried having both std::string, and const char* as template types, but the compiler ( and by extension, I'm guessing, the standard) will not allow me to use string literals as template parameters.I tried placing static strings in the public sections of the XmlNode class, then access them with XmlNode<...>::str = "Tha... yet that gave me the error:expected constructor, destructor, or type conversion before '=' tokenDoes anyone have a method of doing this, or am I to resign myself to the fact that I can supply a name to only the top-most layer of my XML?If anyone's interested, here is the code;XmlNodetemplate < class Attribute, class ChildElement, class Sibling >class XmlNode{   std::string str;   Attribute m_attr;   ChildElement m_child;   Sibling m_sib;    public:   XmlNode(std::string str):str(str){}   XmlNode(){}   void operator()(std::string str)   {  this->str=str;   }   void Out(std::ostream& out)   {  out << "<" << str.c_str();  m_attr.Out(out);  out << ">" << std::endl;  m_child.Out(out);  out << "</" << str.c_str() << ">" << std::endl; m_sib.Out(out);   }};the main() function, with typedefs;typedef XmlNode< XmlBlank, XmlBlank, XmlBlank > XmlTag;typedef XmlNode< XmlBlank, XmlTag, XmlTag > XmlNested;typedef XmlNode< XmlBlank, XmlNested, XmlBlank > XmlDoc;//typedef XmlNode< XmlBlank, XmlNode< XmlBlank, XmlNode< XmlBlank, XmlBlank, XmlBlank >, XmlNode< XmlBlank, XmlBlank, XmlBlank > >, XmlBlank > XmlDoc;XmlDoc::str = "Document";int main(){std::ofstream out("XmlTestFile.xml");XmlDoc _xml_doc("Document");XmlTag _xml_tag("Tag");out << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" << std::endl;_xml_doc.Out(out);_xml_tag.Out(out);out.close();std::cout << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" << std::endl;_xml_doc.Out(std::cout);return 0;}And the file it produces;<?xml version="1.0" encoding="ISO-8859-1"?><Document><><></></><></></Document><Tag></Tag>If anyone has any advice, it would be greatly appreciated.   
  Non-integral non-type template parameters must be instantiated with entities with external linkage. String literals have implicit internal linkage, so can't be used as non-type template parameters. You can, however, do something like:#include <iostream>template <const char *& str>struct A {  A() { std::cout << str << std::endl; }};extern const char * extern_string = "fred";int main(int, char **) {  A<extern_string> a;  return 0;};  In your code you don't seem to be using strings as template parameters, you're just passing them into the constructor and () operator and both appear to be working as they should.    If you look at the output the document is doing exactly what you told it to do and displaying "Document",going into the Nested and then Tag class and displaying the "<" icons etc... as far as I can see it is doing what you told it to do... but i'm not sure what your code is supposed to do so I can't really help you with the output...   If you intended the Tag class within the _xml_doc object to be the same one bearing the string that says "Tag" you might want to make some changes to your Node so that it uses pointers instead of objects, and passing said pointers into the constructor. You cannot use individual strings as template parameters anyway, though you don't appear to be doing so anyway. Passing them into functions of a template class is perfectly legal.   ;  Ah I think I see what you are trying to do now... yeah just use the method SirCrane mentioned, add a const char* parameter to your template, and pass in  desired name in your typedef.   ;  OK, thanks. I'll try that when I get home from work.Just noticed, my code I put up wouldn't compile anyway. I forgot to remove XmlDoc::str = "Document";AndyPandyV2: Yeah, sorry, I was showing how far I'd successfully got. I didn't show the different methods I'd tried to get to work, but failed.SiCrane: is the *& intended? what exactly does it do? It lloks like a pointer to a reference[Edited by - webwraith on April 18, 2008 4:25:02 AM];  It's a reference to a pointer, and, yes, it was intended.   