Tag: 模板

为什么派生的模板类不能访问基本模板类的标识符?

考虑: template <typename T> class Base { public: static const bool ZEROFILL = true; static const bool NO_ZEROFILL = false; } template <typename T> class Derived : public Base<T> { public: Derived( bool initZero = NO_ZEROFILL ); // NO_ZEROFILL is not visible ~Derived(); } 我不能用GCC g ++ 3.4.4(cygwin)编译这个。 在将它们转换为类模板之前,它们是非generics的,派生类能够看到基类的静态成员。 在C ++规范的要求中是否会丢失可见性,或者是否需要使用语法更改? 我知道Base<T>每个实例都有它自己的静态成员“ NO_ZEROFILL ”和“ NO_ZEROFILL […]

从模板函数调用的模板类的模板成员函数

这不会编译: template<class X> struct A { template<int I> void f() {} }; template<class T> void g() { A<T> a; af<3>(); // Compilation fails here (Line 18) } int main(int argc, char *argv[]) { g<int>(); // Line 23 } 编译器(gcc)说: hhh.cpp:在函数'void g()': hhh.cpp:18:错误:在')'标记之前预期的主expression式 hhh.cpp:在函数'void g()[with T = int]': hhh.cpp:23:从这里实例化 hhh.cpp:18:错误:使用成员无效(你忘了'&'?) 任何人都可以解释为什么这是? 有没有办法让它起作用?

元程序化:函数定义的失败定义了一个独立的函数

在这个答案中,我定义了一个基于types的is_arithmetic属性的模板: template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){ return to_string(t); } template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); } dyp表明 ,而不是该types的is_arithmetic属性,是否为该types定义了to_string是模板select条件。 这显然是可取的,但我不知道这样说: 如果未定义std::to_string则使用ostringstream重载。 声明to_string条件很简单: template<typename T> decltype(to_string(T{})) stringify(T t){ return to_string(t); } 这是与我无法弄清楚如何构build的标准相反的。 这显然不起作用,但希望它传达了我想要构build的东西: template<typename T> enable_if_t<!decltype(to_string(T{})::value, string> (T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); }

一个模板类中的模板函数的显式特化的C + +语法?

我有代码在VC9(Microsoft Visual C ++ 2008 SP1),但不是在GCC 4.2(在Mac): struct tag {}; template< typename T > struct C { template< typename Tag > void f( T ); // declaration only template<> inline void f< tag >( T ) {} // ERROR: explicit specialization in }; // non-namespace scope 'structC<T>' 我明白海湾合作委员会希望我在课堂之外移动我的显式专业,但是我无法弄懂语法。 有任何想法吗? // the following is not correct […]

C ++模板构造函数

我希望有一个不带参数的模板构造函数的非模板类。 据我所知,这是不可能的(因为它会违反默认的构造函数 – 我是吗? ), 解决方法如下: class A{ template <typename U> A(U* dummy) { // Do something } }; 也许有更好的替代scheme(或更好的解决方法)? 谢谢。

为什么我不能使用float值作为模板参数?

当我尝试使用float作为模板参数时,编译器为此代码而哭泣,而int工作正常。 是否因为我不能使用float作为模板参数? #include<iostream> using namespace std; template <class T, T defaultValue> class GenericClass { private: T value; public: GenericClass() { value = defaultValue; } T returnVal() { return value; } }; int main() { GenericClass <int, 10> gcInteger; GenericClass < float, 4.6f> gcFlaot; cout << "\n sum of integer is "<<gcInteger.returnVal(); cout << "\n sum […]

尾随返回types使用带有可变参数模板函数的decltype

我想写一个简单的加法器(笑),加起来每个参数,并返回与适当types的总和。 目前,我有这样的: #include <iostream> using namespace std; template <class T> T sum(const T& in) { return in; } template <class T, class… P> auto sum(const T& t, const P&… p) -> decltype(t + sum(p…)) { return t + sum(p…); } int main() { cout << sum(5, 10.0, 22.2) << endl; } 在GCC 4.5.1上,这对于2个参数来说似乎工作得很好,例如sum(2,5.5)返回7.5。 然而,比这更多的争论,我得到错误sum()是根本没有定义。 如果我这样宣布sum(): […]

为什么不从构造函数推断模板参数?

我今天的问题很简单:编译器为什么不能从类构造函数中推断出模板参数,就像从函数参数中可以这样做呢? 例如,为什么以下代码无效: template<typename obj> class Variable { obj data; public: Variable(obj d) { data = d; } }; int main() { int num = 2; Variable var(num); //would be equivalent to Variable<int> var(num), return 0; //but actually a compile error } 正如我所说,我明白这是无效的,所以我的问题是为什么不是? 会允许这个创build任何主要的语法漏洞? 是否有一个不希望这个function的实例(推断types会导致问题)? 我只是想了解允许模板推理function背后的逻辑,但不适合适当构build的类。

为什么模板论证扣除在这里不起作用?

我创build了两个获取模板参数的简单函数和一个定义types的空结构: //S<T>::type results in T& template <class T> struct S { typedef typename T& type; }; //Example 1: get one parameter by reference and return it by value template <class A> A temp(typename S<A>::type a1) { return a1; } //Example 2: get two parameters by reference, perform the sum and return it template <class A, […]

SFINAE工作返回types,但不作为模板参数

我已经使用了SFINAE成语很多次了,我习惯于将std::enable_if<>放在模板参数中,而不是返回types中。 然而,我遇到了一个不起作用的小事,我不知道为什么。 首先,这是我的主要: int main() { foo(5); foo(3.4); } 这里是触发错误的foo的实现: template<typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> auto foo(T) -> void { std::cout << "I'm an integer!\n"; } template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> auto foo(T) -> void { std::cout << "I'm a floating point number!\n"; } 这是一个可以正常工作的代码: template<typename T> auto foo(T) -> typename std::enable_if<std::is_integral<T>::value>::type { […]