Skip to content
Snippets Groups Projects
Commit ff4214dd authored by Serge Pavlov's avatar Serge Pavlov
Browse files

Do not inherit default arguments for friend function in class template.

A function declared in a friend declaration may have declarations prior
to the containing class definition. If such declaration defines default
argument, the friend function declaration inherits them. This behavior
causes problems if the class where the friend is declared is a template:
during the class instantiation the friend function looks like if it had
default arguments, so error is triggered.

With this change friend functions declared in class templates do not
inherit default arguments. Actual set of them will be defined at the
point where the containing class is instantiated.

This change fixes PR12724.

Differential Revision: https://reviews.llvm.org/D30393


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@304965 91177308-0d34-0410-b5e6-96231b3b80d8
parent 14318c6a
No related branches found
No related tags found
No related merge requests found
...@@ -547,17 +547,23 @@ bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, ...@@ -547,17 +547,23 @@ bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old,
Diag(OldParam->getLocation(), diag::note_previous_definition) Diag(OldParam->getLocation(), diag::note_previous_definition)
<< OldParam->getDefaultArgRange(); << OldParam->getDefaultArgRange();
} else if (OldParamHasDfl) { } else if (OldParamHasDfl) {
// Merge the old default argument into the new parameter. // Merge the old default argument into the new parameter unless the new
// It's important to use getInit() here; getDefaultArg() // function is a friend declaration in a template class. In the latter
// strips off any top-level ExprWithCleanups. // case the default arguments will be inherited when the friend
NewParam->setHasInheritedDefaultArg(); // declaration will be instantiated.
if (OldParam->hasUnparsedDefaultArg()) if (New->getFriendObjectKind() == Decl::FOK_None ||
NewParam->setUnparsedDefaultArg(); !New->getLexicalDeclContext()->isDependentContext()) {
else if (OldParam->hasUninstantiatedDefaultArg()) // It's important to use getInit() here; getDefaultArg()
NewParam->setUninstantiatedDefaultArg( // strips off any top-level ExprWithCleanups.
OldParam->getUninstantiatedDefaultArg()); NewParam->setHasInheritedDefaultArg();
else if (OldParam->hasUnparsedDefaultArg())
NewParam->setDefaultArg(OldParam->getInit()); NewParam->setUnparsedDefaultArg();
else if (OldParam->hasUninstantiatedDefaultArg())
NewParam->setUninstantiatedDefaultArg(
OldParam->getUninstantiatedDefaultArg());
else
NewParam->setDefaultArg(OldParam->getInit());
}
} else if (NewParamHasDfl) { } else if (NewParamHasDfl) {
if (New->getDescribedFunctionTemplate()) { if (New->getDescribedFunctionTemplate()) {
// Paragraph 4, quoted above, only applies to non-template functions. // Paragraph 4, quoted above, only applies to non-template functions.
......
...@@ -73,3 +73,36 @@ void Test () ...@@ -73,3 +73,36 @@ void Test ()
} }
} // namespace } // namespace
namespace pr12724 {
void func_01(bool param = true);
class C01 {
public:
friend void func_01(bool param);
};
void func_02(bool param = true);
template<typename T>
class C02 {
public:
friend void func_02(bool param);
};
C02<int> c02;
void func_03(bool param);
template<typename T>
class C03 {
public:
friend void func_03(bool param);
};
void func_03(bool param = true);
C03<int> c03;
void main() {
func_01();
func_02();
func_03();
}
} // namespace pr12724
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment