Hello,

je bloque sur un petit problème : j'ai besoin d'une classe contenant un tableau de pointeur de fonctions, les fonctions pointées sont des membres de la classe de base.

Dans le cas où la classe de base n'est pas template je n'ai pas de problème, lorsqu'elle l'est par contre ...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <array>
#include <functional>
#include <initializer_list>
#include <iostream>
 
template <class T>
struct base {
	template <int I>
	const T& foo(const T& t) {
		std::cout << I << std::endl;
		return t;
	}
};
 
template <class T, int... Is>
struct derivee: base<T> {
	using arr_type = std::array<std::function<const T&(const T&)>, sizeof...(Is)>;
 
	arr_type fct;
 
	derivee():
		fct {{ std::bind(&derivee::foo<Is>, this, std::placeholders::_1)... }}
	{ }
};
 
int main() {
	int i = 2;
	derivee<int, 1, 2, 42> d;
 
	for(auto f: d.fct) {
		f(i);
	}
 
	return 0;
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
g++-4.8 -o test test.cpp -std=c++11
test.cpp: In constructor ‘derivee<T, Is>::derivee()’:
test.cpp:22:37: error: expected primary-expression before ‘,’ token
   fct {{ std::bind(&derivee::foo<Is>, this, std::placeholders::_1)... }}
                                     ^
test.cpp: In instantiation of ‘derivee<T, Is>::derivee() [with T = int; int ...Is = 1, 2, 42]’:
test.cpp:28:25:   required from here
test.cpp:22:33: error: address of overloaded function with no contextual type information
   fct {{ std::bind(&derivee::foo<Is>, this, std::placeholders::_1)... }}
                                 ^
Je peux m'en sortir en plaçant foo dans derivee, et donc sans héritage. Mais les spécialisation de foo seraient plus chiante à écrire.
(Puis je suis currieux de savoir pourquoi ça marche pas si la classe de base est template)