Restrict template friends of a class

Viewed 84

Consider the following code:

#include <iostream>

class S {
    static const int i = 42;
    template <class T>
    friend void f();
};

template <class T>
void f()
{
    std::cout << S::i << "\n";
}

int main() 
{
    f<int>();
    f<double>();
}

All I want here is to allow access to private part of a class S to f<int>, but not for f<double>. I.e. I want to get compiler error like 'i' is a private member of 'S' for f<double>() line.

How to achive this?

2 Answers
Related