Skip to content

Commit 0fd737a

Browse files
Refs #14622: fix wrong template instantiation with specialization (danmar#8373)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent b599bb4 commit 0fd737a

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

lib/templatesimplifier.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2085,8 +2085,11 @@ void TemplateSimplifier::expandTemplate(
20852085
std::stack<const Token *> templates;
20862086
int scopeCount = 0;
20872087
for (; tok3; tok3 = tok3->next()) {
2088-
if (tok3->str() == "{")
2088+
if (tok3->str() == "{") {
2089+
if (isFunction && isSpecialization && inTemplateDefinition)
2090+
break;
20892091
++scopeCount;
2092+
}
20902093
else if (tok3->str() == "}")
20912094
--scopeCount;
20922095
if (scopeCount < 0)

test/testsimplifytemplate.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class TestSimplifyTemplate : public TestFixture {
225225
TEST_CASE(template181);
226226
TEST_CASE(template182); // #13770
227227
TEST_CASE(template183);
228+
TEST_CASE(template184);
228229
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
229230
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
230231
TEST_CASE(template_specialization_3);
@@ -4698,6 +4699,32 @@ class TestSimplifyTemplate : public TestFixture {
46984699
ASSERT_EQUALS(exp, tok(code));
46994700
}
47004701

4702+
void template184() {
4703+
const char code[] = "template <typename T>\n"
4704+
"T g(T x) {\n"
4705+
" return x;\n"
4706+
"}\n"
4707+
"template <>\n"
4708+
"float g<float>(float x) {\n"
4709+
" return x + 1.0f;\n"
4710+
"}\n"
4711+
"void f(int i) {\n"
4712+
" g(i);\n"
4713+
" g(1.0f);\n"
4714+
"}\n";
4715+
const char exp[] = "float g<float> ( float x ) ; "
4716+
"template < typename T > "
4717+
"T g ( T x ) { return x ; } "
4718+
"float g<float> ( float x ) {"
4719+
" return x + 1.0f ; "
4720+
"} "
4721+
"void f ( int i ) {"
4722+
" g ( i ) ;"
4723+
" g<float> ( 1.0f ) ; "
4724+
"}";
4725+
ASSERT_EQUALS(exp, tok(code)); // TODO: instantiate g<int>(int)
4726+
}
4727+
47014728
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
47024729
const char code[] = "template <typename T> struct C {};\n"
47034730
"template <typename T> struct S {a};\n"

0 commit comments

Comments
 (0)