a template confuseness

Because there is much code, I will give you a link, you can download it:

http://pan.baidu.com/s/1nufhMLb#path=%252F

If you download it, you can use CMake to generate the project quickly.

the source.cpp has the following code:
1
2
3
4
5
6
7
	GenScatterHierarchy<TYPELIST_03(int, int,int), Holder> class_02;
	SafeField<0>(class_02).value_ = 1;
	SafeField<1>(class_02).value_ = 2;
	SafeField<2>(class_02).value_ = 3;
	std::cout << SafeField<0>(class_02).value_ << std::endl;
	std::cout << SafeField<1>(class_02).value_ << std::endl;
	std::cout << SafeField<2>(class_02).value_ << std::endl;


I think, there must be an error when compiling SafeField<0>(class_02), because
GenScatterHierarchy<int, Holder> has been inherited several times, when implicitly conveting to GenScatterHierarchy<int, Holder>, it is ambiguous. But the c++ complier in visual studio 2015 only give a warning.
Because there is much code, I will give you a link, you can download it:


Not many people wish to download a zip file.

Can you post the just the relevant code here: The declaration and implementations of GenScatterHierarchy , TYPELIST_03(int, int,int) , Holder , SafeField<0>(class_02)

Ideally, a minimal but complete and compile-able program that demonstrates the problem. In making such a small program, you might discover the problem :+)

Please note that I may not be able to help with your actual problem: I am just trying to get more relevant information so that perhaps someone way more knowledgeable than me may be better equipped to answer your question.

Edit: Also provide any overloads that you think may contribute to any ambiguity.

Edit2: Try other compilers: try pasting your code into cpp.sh and set all 3 warning levels and C++14. It may come up with warnings that VS doesn't - maybe more clues :+) I like the clang++ compiler from llvm, it has nice easy to understand compiler messages. clang can be integrated with VS : http://www.cplusplus.com/forum/general/184366/#msg901434

http://www.cpp.sh/
Last edited on
It's size is only 10Kb
Hi,

It's not the size, it's because it's an executable file - There is all kinds of danger with this type of file.

I would have thought it just as easy to post the relevant code snippets here :+)

How did you get along with some of my other suggestions?

Regards :+)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
template<class T, class U>
struct TypeList
{
	typedef T Head;
	typedef U Tail;
};
#define TYPELIST_01(T1) TypeList<T1,NullType>
#define TYPELIST_02(T1,T2) TypeList<T1,TYPELIST_01(T2) >
#define TYPELIST_03(T1, T2, T3) TypeList<T1,TYPELIST_02(T2, T3) >
template<class TList, template<class>class Unit>
class GenScatterHierarchy;


template<class Head, class Tail, template<class>class Unit>
class GenScatterHierarchy<TypeList<Head,Tail>, Unit > : public GenScatterHierarchy<Head, Unit>, public GenScatterHierarchy<Tail, Unit>
{
};


template<class T, template<class>class Unit>
class GenScatterHierarchy : public Unit<T>
{
};


template<template<class>class Unit>
class GenScatterHierarchy<NullType, Unit>
{};


template<class T>
struct Holder
{
	T value_;
};

template<class TList, template<class> class Unit>
Unit<typename TList::Head>& SafeFieldHelper(GenScatterHierarchy<TList, Unit>& obj, Int2Type<0>)
{
	GenScatterHierarchy<TList::Head, Unit>& leftbase = obj;
	return leftbase;
}

template<unsigned index , class TList, template<class> class Unit>
Unit<typename TypeAt<TList,index>::Result>& SafeFieldHelper(GenScatterHierarchy<TList, Unit>& obj, Int2Type<index>)
{
	GenScatterHierarchy<TList::Tail, Unit>& rightbase = obj;
	return SafeFieldHelper(rightbase, Int2Type<index-1>());
}

template<unsigned index, class TList, template<class> class Unit>
Unit<typename TypeAt<TList, index>::Result>& SafeField(GenScatterHierarchy<TList, Unit>& obj)
{
	return SafeFieldHelper(obj, Int2Type<index>());
}
Last edited on
'TypeAt<TList, index>::Result' can give TypeList's type at index location
If you want to get all complete implematation of all template and class, you can download the .zip file, it is not .exe files, but some source and .txt files.
you can download the .zip file, it is not .exe files


The zip file itself is an exe file, who knows what viruses or root kits have been injected into it, along the way. Not saying you have done that, maybe someone else. So just post the individual files themselves on that pastebin site. Then no problem downloading them.

As for your problem: as mentioned earlier - too tricky for me :+)
I am concerned about the problem. Can you trust others? I spend so long time just to transit a virus to your computer???
Last edited on
I would really suggest not to make such complicated relation/template. It is way too hard to find a problem even for yourself. What is the use of this template?

One potential problem is the multiple inheritance on line 15. Making th base classes virtual may help.


See:
http://www.cprogramming.com/tutorial/multiple_inheritance.html
Can you trust others? I spend so long time just to transit a virus to your computer???


Just to be clear: I am not saying you have put a virus in the file. On the contrary, someone else may have infected your zip file with a virus through the website, or by some other means.

The main intention of my comment all along was to suggest not posting zip files: anyone with any sense won't open them.
Topic archived. No new replies allowed.