C++程式設計語言練習4.6 區域性名字和外部名字的長度限制
阿新 • • 發佈:2019-01-09
書中程式碼,利用模板副檔名字長度
#include <iostream>
#include <typeinfo>
template<typename T,typename U>
struct Doublify{};
template<int N>
struct Exponential
{
typedef Doublify<typename Exponential<N - 1>::LongType,
typename Exponential<N - 1>::LongType> LongType;
};
template<>
struct Exponential<0>
{
typedef double LongType;
};
int f(Exponential<10>::LongType* ) {
return 3;
}
int main(int argc, char* argv[])
{
Exponential<10>::LongType* ec = new Exponential<10>;
std::cout<<typeid(ec).name<<std::endl;
int a =f(ec);
std::cout<<a<<std::endl;
return 0;
}
編譯執行結果:好像是成功了,但是無法打印出來。
[[email protected] cpp]$ g++ -o 4.6 ./4.6.cpp
./4.6.cpp: 在函式‘int main(int, char**)’中:./4.6.cpp:25:38: 錯誤:不能在初始化時將‘Exponential<10>*’轉換為‘Exponential<10>::LongType* {aka Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > >, Doublify<Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > >, Doublify<Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > >, Doublify<Doublify<Doublify<double, double>, Doublify<double, double> >, Doublify<Doublify<double, double>, Doublify<double, double> > > > > > > > > >*}’
Exponential<10>::LongType* ec = new Exponential<10>;
^~~~~~~~~~~~~~~
./4.6.cpp:26:11: 錯誤:對非靜態成員函式‘const char* std::type_info::name() cons’的使用無效
std::cout<<typeid(ec).name<<std::endl;
~~~~~~~~~^~~~~~~~~~~~~~~~~
In file included from ./4.6.cpp:2:0:
/usr/local/include/c++/6.1.0/typeinfo:99:17: 附註:在此宣告
const char* name() const _GLIBCXX_NOEXCEPT
^~~~
[ [email protected] cpp]$
請教高手,怎麼打印出結果來?