C++ 模板类的模板友元中的依赖项
在阅读《标准艺术》几个小时试图找出这种语法之后,我放弃了理解它。 我有一个模板类,其中有一个模板构造函数我需要部分专门化,因为这是不可能的,我将构造函数实现包装在不同的模板中:C++ 模板类的模板友元中的依赖项,c++,templates,friend,C++,Templates,Friend,在阅读《标准艺术》几个小时试图找出这种语法之后,我放弃了理解它。 我有一个模板类,其中有一个模板构造函数我需要部分专门化,因为这是不可能的,我将构造函数实现包装在不同的模板中: template<class T> struct A; //forward declaration namespace impl { template<class T> struct wrap { template<class U> struct AConstruct
template<class T>
struct A; //forward declaration
namespace impl {
template<class T>
struct wrap {
template<class U>
struct AConstructor {
static void construct(A<T>*thisPtr) {...}
};
//some partial specialization...
};
template<class T>
struct dummy{};
} //end of namespace
template<class T>
struct A {
template<class U>
A(dummy<U>) {
impl::template wrap<T>::template AConstructor<U>::construct(this);
}
};
如何将此函数/结构关联起来?[编辑:
以下内容适用于我的机器上的g++4.9.0和clang 3.5:
template<class T>
struct dummy {};
template<class T>
struct A {
private:
template<class U>
struct AConstructor {
static void construct(A<T>* thisPtr) {
thisPtr->foo = 42;
}
};
template<class>
friend class AConstructor;
public:
template<class U>
A(dummy<U>) {
AConstructor<U>::construct(this);
}
void print() const {
std::cout << foo << std::endl;
}
private:
int foo;
};
int main() {
A<void> foobar( (impl::dummy<void>()) );
foobar.print();
return 0;
}
模板
结构虚拟{};
模板
结构A{
私人:
模板
结构体{
静态空心构造(A*thisPtr){
这个ptr->foo=42;
}
};
模板
友元类构造器;
公众:
模板
A(假人){
构造器::构造(this);
}
void print()常量{
std::cout试试看它是否有效?:template friend void impl::wrap::AConstructor::construct(A*);实际上,我想到的是带有类型特征的设计,问题是我使用的是gcc 4.1,所以我不能使用大多数类型特征(有些是我自己实现的).这个特殊化是为了检查一些继承,所以我自己无法实现它,所以我求助于部分特殊化。
template<class T>
struct dummy {};
template<class T>
struct A {
private:
template<class U>
struct AConstructor {
static void construct(A<T>* thisPtr) {
thisPtr->foo = 42;
}
};
template<class>
friend class AConstructor;
public:
template<class U>
A(dummy<U>) {
AConstructor<U>::construct(this);
}
void print() const {
std::cout << foo << std::endl;
}
private:
int foo;
};
int main() {
A<void> foobar( (impl::dummy<void>()) );
foobar.print();
return 0;
}
template<class T>
struct A; //forward declaration
namespace impl {
template<class T>
struct wrap {
template<class U>
struct AConstructor {
static void construct(A<T>* thisPtr) {
thisPtr->foo = 42;
}
};
//some partial specialization...
};
template<class T>
struct dummy{};
} //end of namespace
template<class T>
struct A {
template<class>
friend class impl::wrap<T>::AConstructor;
template<class U>
A(impl::dummy<U>) {
impl::template wrap<T>::template AConstructor<U>::construct(this);
}
void print() const {
std::cout << foo << std::endl;
}
private:
int foo;
};
int main() {
A<void> foobar( (impl::dummy<void>()) );
foobar.print();
return 0;
}
#include <list>
#include <vector>
template<class T>
struct is_vector : std::false_type {};
template<class T>
struct is_vector<std::vector<T>> : std::true_type {};
struct A {
template<
class T, typename std::enable_if<is_vector<T>::value, void*>::type = nullptr
>
A(T) {
std::cout << "construction with a vector" << std::endl;
}
template<class T>
A(std::list<T>) {
std::cout << "construction with a list of " << typeid(T).name();
std::cout << std::endl;
}
template<
class T,
typename std::enable_if<!is_vector<T>::value, void**>::type = nullptr
>
A(T) {
std::cout << "construction with an iterator (supposedly) whose value_type "
"is";
std::cout << typeid(typename std::iterator_traits<T>::value_type).name();
std::cout << std::endl;
}
};
int main() {
A(std::vector<int>());
A(std::list<std::vector<int>>());
A((char*) nullptr);
return 0;
}