LINUX.ORG.RU

C++ и шаблонный конструктор


0

1
template <int X>
struct A
{
	template <int Y>
	static int mul() { return X * Y; }
	
	template <int Y>
	A() : value(X * Y) {}
	
	int value;
};

int main(int argc, char ** argv)
{
	int value = A<5>::mul<7>();
	A<5> ... <7> ... a ... ?
	return 0;
}

Обычный шаблонный класс. Вызвать его статический шаблонный метод просто (первая строчка в main), а вот как вызвать шаблонный конструктор? В результате a.value должно равняться 35.

★★★★★

Из черновика стандарта от 2012-02-28, параграф 14.5.2.5:

Note: Because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates.

Begemoth ★★★★★
()
Ответ на: комментарий от Begemoth

Спасибо. Пришлось делать с помощью статических инициализаторов.

Dendy ★★★★★
() автор топика

Можно так еще

template <int N>
class A {
public:
	template <int Y>
	class B {
	public:
		int foo(){
			return N*Y;
		}
	};
};

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.