当模板公开继承自另一个模板时,不是应该可访问的基本公共方法吗?
template <int a>
class Test {
public:
Test() {}
int MyMethod1() { return a; }
};
template <int b>
class Another : public Test<b>
{
public:
Another() {}
void MyMethod2() {
MyMethod1();
}
};
int main()
{
Another<5> a;
a.MyMethod1();
a.MyMethod2();
}
Run Code Online (Sandbox Code Playgroud)
好吧,海湾合作委员会对此嗤之以鼻......我必须遗漏一些完全明显的东西(大脑融化).救命?
我曾经认为'typedef'不会自动继承.但下面的代码表示不同的东西.
#include <iostream>
#include <type_traits>
struct A
{
typedef int X;
};
struct A_
{
typedef char X;
};
struct B : A {};
struct B_ : A, A_ {};
template< typename ... Ts >
using void_t = void;
template< typename T, typename = void >
struct has_typedef_X : std::false_type {};
template< typename T >
struct has_typedef_X< T, void_t<typename T::X> > : std::true_type {};
int main()
{
std::cout << std::boolalpha;
std::cout << has_typedef_X<A>::value << std::endl;
std::cout << has_typedef_X<A_>::value << std::endl; …Run Code Online (Sandbox Code Playgroud) 我使用我不熟悉的C++结构回答了这个问题.我想知道这是合法的还是g ++(6.3.0)和clang ++(3.5.0)都是错误的.该示例可在线获取:
#include <iostream>
template <typename T>
struct Base
{
using Type = int;
};
template <typename T>
struct intermediate : Base<T>
{
// 'Type' is not defined here, which is fine
};
template <typename T>
struct Derived : intermediate<T>
{
using Type = typename Derived<T>::Type; // Is this legal?
// using Type = typename intermediate<T>::Type; // Normal way of doing it
};
int main()
{
Derived<void>::Type b = 1;
std::cout << b << std::endl; …Run Code Online (Sandbox Code Playgroud) 我写了一个OutputIterator来回答另一个问题.这里是:
#include <queue>
using namespace std;
template< typename T, typename U >
class queue_inserter {
queue<T, U> &qu;
public:
queue_inserter(queue<T,U> &q) : qu(q) { }
queue_inserter<T,U> operator ++ (int) { return *this; }
queue_inserter<T,U> operator * () { return *this; }
void operator = (const T &val) { qu.push(val); }
};
template< typename T, typename U >
queue_inserter<T,U> make_queue_inserter(queue<T,U> &q) {
return queue_inserter<T,U>(q);
}
Run Code Online (Sandbox Code Playgroud)
这适用于这个小复制功能:
template<typename II, typename OI>
void mycopy(II b, II e, OI oi) {
while …Run Code Online (Sandbox Code Playgroud) 可能的重复:
GCC问题:使用依赖于模板参数的基类成员
为什么当VS没有时,GCC在模板中需要额外的声明?
为什么派生模板类不能访问基本模板类
iphone编译器继承模板化基类,传递类型未及时扩展(只看)
抱歉这个令人困惑的标题,我能想出最好的.
这里有一些代码来说明我的问题......
基本模板类:
template<class T> class TestBase
{
public:
int someInt;
};
Run Code Online (Sandbox Code Playgroud)
试图用另一个模板类继承TestBase ...
这在编译时得到"someInt未在此范围内声明":
template<class X> class TestSub : public TestBase<X>
{
void testf()
{
someInt = 0; //Error: "someInt was not declared in this scope"
}
};
Run Code Online (Sandbox Code Playgroud)
B)这很好用(不同之处在于我明确指定了TestBase的模板输入)
template<class X> class TestSub : public TestBase<string>
{
void testf()
{
someInt = 0;
}
};
Run Code Online (Sandbox Code Playgroud)
为什么(A)的TestSub没有像(B)中那样正确地继承someInt?
提前致谢.
编辑:发现重复
我已经将一些问题代码缩减到最简单的工作案例来说明以下内容:我在纯抽象基类中的typedef不是由派生类继承的.在下面的代码中,我想将system_ttypedef 继承到ConcreteTemplateMethod:
#include <iostream>
// pure abstract template-method
template <typename T> // T == Analyzer<U>
class TemplateMethod {
public:
typedef T system_t;
virtual void fn (const system_t& t) const = 0;
};
template <typename T>
class Analyzer {
public:
void TemplatedAlgorithm (const TemplateMethod< Analyzer <T> >& a) const {
printf ("Analyzer::TemplatedAlgorithm\n");
a.fn(*this); // run the template-method
}
void fn () const {
printf ("Analyzer::fn\n");
}
};
// concrete template-method
template <typename T>
class ConcreteTemplateMethod …Run Code Online (Sandbox Code Playgroud) c++ inheritance design-patterns typedef template-method-pattern
我偶然发现了一个错误,它只出现在GCC 6.2.0上,而不是出现在Clang 3.9.0上(都在-std=c++14模式下).我不确定哪种行为是正确的(以及我是否应该提交错误).
这是代码:
template<typename type_t>
class foo_t
{
};
class bar_t
{
public:
using foo_t = int;
};
class baz_t:
public bar_t
{
private:
template<typename type_t>
friend class foo_t;
};
Run Code Online (Sandbox Code Playgroud)
在GCC上,这会出现以下错误:
test.cpp:17:15: error: using typedef-name ‘using foo_t = int’ after ‘class’
friend class foo_t;
^~~~~
test.cpp:9:19: note: ‘using foo_t = int’ has a previous declaration here
using foo_t = int;
^
Run Code Online (Sandbox Code Playgroud)
根据我所知的C++标准,父级typedef(或usings)不应泄漏到子级的范围内,您需要明确限定名称:例如,请参阅将"typedef"从基于"模板"的派生类传播到"模板".所以在我看来,GCC在这里是不正确的,但我不太确定我的C++知识可以自信地说.
谢谢你的帮助!
如何定义从模板类继承的模板类?
我想包装std::queue并std::priority_queue转到基类。就我而言LooperQueue。我用StdQueue这种方式auto queue = new StdQueue<LooperMessage *>()。
我的课定义编译器抱怨
错误日志:
In file included from /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/Painter.cpp:10:
/Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:14:5: error: unknown type name 'size_type'; did you mean 'size_t'?
size_type size() override;
^~~~~~~~~
size_t
/Users/rqg/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/5.0.300080/include/stddef.h:62:23: note: 'size_t' declared here
typedef __SIZE_TYPE__ size_t;
^
In file included from /Users/rqg/ASProjects/PboTest/muses/src/main/cpp/Painter.cpp:10:
/Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:16:5: error: unknown type name 'reference'
reference front() override;
^
/Users/rqg/ASProjects/PboTest/muses/src/main/cpp/util/StdQueue.h:20:21: error: unknown type name 'value_type'; did you mean 'ARect::value_type'?
void push(const value_type &x) override;
^~~~~~~~~~
ARect::value_type …Run Code Online (Sandbox Code Playgroud) 平台:MinGW64(rubenvb 4.7.2),Windows 7(64),Qt 4.8.2
给定代码段如下:
/* type definition */
typedef long T_PSIZE;
struct A { T_PSIZE myArray[10]; };
struct B { T_PSIZE myArray[10]; };
/* declare variable */
A a;
B b;
std::copy(a.myArray[0], a.myArray[10], &b.myArray);
Run Code Online (Sandbox Code Playgroud)
我不知道为什么编译器抛出以下错误消息(当从'typedef long T_PSIZE''更改为'typedef int T_PSIZE'时,也会显示类似的消息):
> c:\mingw\rubenvb-4.7.2-64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:
> In instantiation of '_OI std::__copy_move_a(_II, _II, _OI) [with bool
> _IsMove = false; _II = long int; _OI = long int (*)[9]]': c:\mingw\rubenvb-4.7.2-64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:422:39:
> required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool
> _IsMove = false; _II …Run Code Online (Sandbox Code Playgroud)