Code:
#include <iostream>
using namespace std;
template<typename T, bool onScreen>
class Pi
{
public:
static T lo()
{
return 3.1415926535897932383L;
}
static T up()
{
return 3.1415926535897932385L;
}
static T eps()
{
T result = up() - lo();
if (onScreen) {
cout << result << endl;
}
return result;
}
};
template<bool onScreen>
float Pi<float, onScreen>::lo()
{
return 3.1415925f;
}
template<bool onScreen>
float Pi<float, onScreen>::up()
{
return 3.1415927f;
}
template<bool onScreen>
double Pi<double, onScreen>::lo()
{
return 3.1415926535897931;
}
template<bool onScreen>
double Pi<double, onScreen>::up()
{
return 3.1415926535897935;
}
int main()
{
Pi<double, true>::eps();
return 0;
}
#include <iostream>
using namespace std;
template<typename T, bool onScreen>
class Pi
{
public:
static T lo()
{
return 3.1415926535897932383L;
}
static T up()
{
return 3.1415926535897932385L;
}
static T eps()
{
T result = up() - lo();
if (onScreen) {
cout << result << endl;
}
return result;
}
};
template<bool onScreen>
float Pi<float, onScreen>::lo()
{
return 3.1415925f;
}
template<bool onScreen>
float Pi<float, onScreen>::up()
{
return 3.1415927f;
}
template<bool onScreen>
double Pi<double, onScreen>::lo()
{
return 3.1415926535897931;
}
template<bool onScreen>
double Pi<double, onScreen>::up()
{
return 3.1415926535897935;
}
int main()
{
Pi<double, true>::eps();
return 0;
}
Kako napisati pa da može? Evo šta se uspešno kompajlira:
Code:
#include <iostream>
using namespace std;
template<typename T, bool onScreen>
class Pi
{
public:
static T lo()
{
return 3.1415926535897932383L;
}
static T up()
{
return 3.1415926535897932385L;
}
static T eps()
{
T result = up() - lo();
if (onScreen) {
cout << result << endl;
}
return result;
}
};
template<>
float Pi<float, true>::lo()
{
return 3.1415925f;
}
template<>
float Pi<float, false>::lo()
{
return 3.1415925f;
}
template<>
float Pi<float, true>::up()
{
return 3.1415927f;
}
template<>
float Pi<float, false>::up()
{
return 3.1415927f;
}
template<>
double Pi<double, true>::lo()
{
return 3.1415926535897931;
}
template<>
double Pi<double, false>::lo()
{
return 3.1415926535897931;
}
template<>
double Pi<double, true>::up()
{
return 3.1415926535897935;
}
template<>
double Pi<double, false>::up()
{
return 3.1415926535897935;
}
int main()
{
Pi<double, true>::eps();
return 0;
}
#include <iostream>
using namespace std;
template<typename T, bool onScreen>
class Pi
{
public:
static T lo()
{
return 3.1415926535897932383L;
}
static T up()
{
return 3.1415926535897932385L;
}
static T eps()
{
T result = up() - lo();
if (onScreen) {
cout << result << endl;
}
return result;
}
};
template<>
float Pi<float, true>::lo()
{
return 3.1415925f;
}
template<>
float Pi<float, false>::lo()
{
return 3.1415925f;
}
template<>
float Pi<float, true>::up()
{
return 3.1415927f;
}
template<>
float Pi<float, false>::up()
{
return 3.1415927f;
}
template<>
double Pi<double, true>::lo()
{
return 3.1415926535897931;
}
template<>
double Pi<double, false>::lo()
{
return 3.1415926535897931;
}
template<>
double Pi<double, true>::up()
{
return 3.1415926535897935;
}
template<>
double Pi<double, false>::up()
{
return 3.1415926535897935;
}
int main()
{
Pi<double, true>::eps();
return 0;
}
E, sad, šta da drugi parametar nije bio tipa bool, nego npr. int? Ova doskočica ne bi prošla.
Nije bitno koji su zaključci izvučeni, već kako se do njih došlo.

Zbun oko specijalizacije šablona