Fixed #1798 (structure templates)

This commit is contained in:
Robert Reif 2010-06-16 07:13:52 +02:00 committed by Daniel Marjamäki
parent ae2a02ad93
commit 61e7e759f0
2 changed files with 66 additions and 2 deletions

View File

@ -2134,7 +2134,7 @@ void Tokenizer::simplifyTemplates()
{
if (tok->str() == ">")
{
if (Token::Match(tok, "> class %var%"))
if (Token::Match(tok, "> class|struct %var%"))
classname = tok->strAt(2);
break;
}
@ -2213,7 +2213,7 @@ void Tokenizer::simplifyTemplates()
// get the position of the template name
unsigned int namepos = 0;
if (Token::Match(tok, "> class %type% {|:"))
if (Token::Match(tok, "> class|struct %type% {|:"))
namepos = 2;
else if (Token::Match(tok, "> %type% *|&| %type% ("))
namepos = 2;

View File

@ -104,6 +104,7 @@ private:
TEST_CASE(template18);
TEST_CASE(template19);
TEST_CASE(template20);
TEST_CASE(template21);
TEST_CASE(template_unhandled);
TEST_CASE(template_default_parameter);
TEST_CASE(template_default_type);
@ -1776,6 +1777,69 @@ private:
ASSERT_EQUALS(expected, sizeof_(code));
}
void template21()
{
{
const char code[] = "template <classname T> struct Fred { T a; };\n"
"Fred<int> fred;";
const std::string expected("; ; "
"Fred<int> fred ; "
"struct Fred<int> { int a ; }");
ASSERT_EQUALS(expected, sizeof_(code));
}
{
const char code[] = "template <classname T, int sz> struct Fred { T data[sz]; };\n"
"Fred<float,4> fred;";
const std::string expected("; ; "
"Fred<float,4> fred ; "
"struct Fred<float,4> { float data [ 4 ] ; }");
ASSERT_EQUALS(expected, sizeof_(code));
}
{
const char code[] = "template <classname T> struct Fred { Fred(); };\n"
"Fred<float> fred;";
const std::string expected("; ; "
"Fred<float> fred ; "
"struct Fred<float> { Fred<float> ( ) ; }");
ASSERT_EQUALS(expected, sizeof_(code));
}
{
const char code[] = "template <classname T> struct Fred { };\n"
"template <classname T> Fred<T>::Fred() { }\n"
"Fred<float> fred;";
const std::string expected("; ; "
"; "
"Fred<float> fred ; "
"struct Fred<float> { } "
"Fred<float> :: Fred<float> ( ) { }");
ASSERT_EQUALS(expected, sizeof_(code));
}
{
const char code[] = "template <classname T> struct Fred { };\n"
"Fred<float> fred1;\n"
"Fred<float> fred2;";
const std::string expected("; ;"
" Fred<float> fred1 ;"
" Fred<float> fred2 ;"
" struct Fred<float> { }");
ASSERT_EQUALS(expected, sizeof_(code));
}
}
void template_unhandled()
{
// An unhandled template usage should be simplified..