The private member function's action scope

Viewed 78

The Issue:

Class declaration is as below:

class Select {
public:
    template<typename Iterator>
    static Iterator function(Iterator , Iterator , bool (*judgeFunction)(Iterator A, Iterator B) = priFunction);

private:
    template<typename Iterator>
    inline static bool priFunction(Iterator , Iterator);

And my test file is as below:

template <typename T>
void showFunction(std::vector<T> &A){
    Select S;
    std::cout<<*S.function(A.begin(),A.end())<<std::endl;
}

CLion remarked an error for the test file:

In template: 'priFunction' is a private member of 'Select'

I used the following sentence compiled my CppFile:

g++ -o ExeFile CppFile.cpp -I "../../../Include/"

And its ExeFile ran successfully.

My Question

  1. Can the member function in private or protected scope of a class be declared as a default parameter in the class' public functions' parameter list?
  2. If there's no error in fact,How could I hide the error reminder of CLion?(unecessary)

My Clion version is 2022.1.3.

1 Answers

The program is well-formed as we're allowed to use priFunction as a default argument the way you did in the above example.

This seems to be a clang bug. Demo. As you can see in the above link, clang also give the error:

<source>:6:100: error: 'priFunction' is a private member of 'Select'
    static Iterator function(Iterator , Iterator , bool (*judgeFunction)(Iterator A, Iterator B) = priFunction);
                                                                                                   ^~~~~~~~~~~
<source>:16:17: note: in instantiation of default function argument expression for 'function<__gnu_cxx::__normal_iterator<int *, std::vector<int>>>' required here
    std::cout<<*S.function(A.begin(),A.end())<<std::endl;
                ^
<source>:21:2: note: in instantiation of function template specialization 'showFunction<int>' requested here
 showFunction(v);
 ^
<source>:10:24: note: declared private here
    inline static bool priFunction(Iterator , Iterator){return true;}

GCC and MSVC correctly compiles the program without any problem. Demo

Related