lelumpolelum
lelumpolelum

Reputation: 3

Decltype error - doesn't see method declaration

I'm trying to use decltype, but I got the same error every time when I was trying compile.

#include <iostream>
#include <cmath>

using namespace std;

class Polygon {
private:
    double bok; 
    int katy;
public:
    Polygon(int katy,double bok): katy (katy),bok (bok) {};
void scale(double s){ bok*=s;};
    double area () const{
            return (katy*(bok*bok))/(4.0*tan((M_PI/katy)));}};
        
int main(){
    Polygon polygon(7,10.);
    polygon.scale(2.);
    cout<<polygon.area()<<endl;
    
    if(!std::is_same<decltype(scale),void (Polygon*,double)>::value){
        cout<<'p';
    }
}

I got a error: "

error: ‘scale’ was not declared in this scope; did you mean ‘scalb’?
   28 |     if(!std::is_same<decltype(scale),void (Polygon*,double)>::value){

" What's wrong?

Upvotes: 0

Views: 49

Answers (1)

Aleksandr Medvedev
Aleksandr Medvedev

Reputation: 9008

You need to take a pointer to the member function and prefix it with the class scope in order to refer to it:

if (!std::is_same<decltype(&Polygon::scale), void(Polygon*, double)>::value) {
    cout<<'p';
}

Be advised, however, that member function type is not a plain function pointer, as for each class there is special type defined within the class scope, which follows this pattern: ReturnType(ClassName::*)(args). Thus in your scenario, if you expect std::is_same to match the function, you should specify the conditional expression as follows:

std::is_same_v<
  decltype(&Polygon::scale),
  void(Polygon::*)(double)
>

Upvotes: 1

Related Questions