I am using Eigen linear algebra package, where they provide matrices templated on the dimensions. I would like to have some functions that generate test data for my program, and so I am trying to template them on the dimensions as well, and have specific specialisations:
template <size_t K>
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams()
{
throw "not implemented";
}
template <size_t K>
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<1>()
{
boost::shared_ptr<std::vector<DistParams<K> > > distParams=(new std::vector<DistParams<K> >());
distParams->push_back(DistParams<K>(asMatrix(0.05), asMatrix(0.1)));
distParams->push_back(DistParams<K>(asMatrix(-0.1), asMatrix(0.2)));
return distParams;
}
template <size_t K>
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<2>()
{
boost::shared_ptr<std::vector<DistParams<K> > > distParams=(new std::vector<DistParams<K> >());
Eigen::Vector2d highMu;
lowMu << 0.04 << 0.06;
Eigen::Matrix2d lowSigma;
highSigma << 0.1 << 0.4
<< 0.4 << 0.12;
Eigen::Vector2d lowMu;
lowMu << -0.08 << -0.12;
Eigen::Matrix2d highSigma;
highSigma << 0.2 << 0.7
<< 0.7 << 0.24;
distParams->push_back(DistParams<K>(highMu, lowSigma));
distParams->push_back(DistParams<K>(lowMu, highSigma));
return distParams;
}
however, this stuff doesn't compile. I get:
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:24:73: error: template-id ‘sampleDistParams<1>’ in declaration of primary template
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:24:53: error: redefinition of ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:18:53: error: ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’ previously declared here
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:33:73: error: template-id ‘sampleDistParams<2>’ in declaration of primary template
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:33:53: error: redefinition of ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’
/home/ga1009/PhD/cpp/pmi/cpp/test/baumiterationtest.cpp:18:53: error: ‘template<unsigned int K> boost::shared_ptr<std::vector<DistParams<K> > > {anonymous}::sampleDistParams()’ previously declared here
What went wrong and how can I fix it?
If you want to specialize a function template, you should not redeclare the template parameters (note that you cannot partially specialize a function template):
template<size_t K> void foo() { } // Primary template
template<size_t K> void foo<1>() { } // ERROR: Redefinition
template<> void foo<1>() { } // OK: Specialization
In your particular case, the right signature for the template specialization is:
template<>
boost::shared_ptr<std::vector<DistParams<1> > > sampleDistParams<1>()
Also notice, that in C++11 the spaces between the closing angle brackets (>) are no more necessary.
wrong syntax:
template specialization should be:
template <>
boost::shared_ptr<std::vector<DistParams<K> > > sampleDistParams<1>()
Related
This is similar to Template specialization with variadic templates, however, that was asked in 2011 and the answer was incomplete support from compiler.
Consider the following code
#include <iostream>
template< typename resource >
struct smart_ref{};
template< class resource, class linked, class... arg_types >
smart_ref< resource > make_smart( linked const& lnk, arg_types&&... args )
{
return smart_ref< resource >{};
}
struct image
{
struct linked {};
};
template<>
smart_ref< image > make_smart< image, image::linked, int const >( image::linked const& lnk, int const arg )
{
return smart_ref< image >{};
}
int main()
{
//auto img = make_smart< image >( image::linked{}, 2 );
return 0;
}
Above code gives following error
$g++ -o main *.cpp
main.cpp:20:20: error: template-id ‘make_smart<image, image::linked, const int>’ for ‘smart_ref<image> make_smart(const image::linked&, int)’ does not match any template declaration
smart_ref< image > make_smart< image, image::linked, int const >( image::linked const& lnk, int const arg )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:9:23: note: candidate is: template<class resource, class linked, class ... arg_types> smart_ref<resource> make_smart(const linked&, arg_types&& ...)
smart_ref< resource > make_smart( linked const& lnk, arg_types&&... args )
^~~~~~~~~~
In template specialization, I've tried with make_smart(...), make_smart< image >(...) and make_smart< image, image::linked > too, but it is still giving above error. What is the correct syntax for template specialization in this case?
specialization must exactly match the template declaration i.e. arg arguments must be int && arg rather than int const arg
I was trying to separate a "generic" map from the event manager implementation, because I need to use it somewhere else. But I ran into something quite unusual for me. So it seems like I'm trying to generate two times (at least) the GetValue function.
#include <tuple>
#include <utility>
#include <vector>
#include <functional>
namespace meta {
template < typename T >
struct CType { using type = T; };
}
namespace containers {
template < template < typename T > class T_Storage,
typename... T_Keys >
class TypedMap {
template < typename T_Key >
using U_Pair = decltype(
std::make_pair(
meta::CType<T_Key>{},
T_Storage<T_Key>{}
));
using U_Map = decltype(
std::make_tuple(
U_Pair<T_Keys>{}
...));
public:
U_Map m_map;
//-----------------------------------------
//! Functions
public:
//!
//! #fn GetValue
//! #brief Acess to map[key]
//! #param type is the key used to find the data
//! #return map[key] reference
//!
template < typename T_Key >
constexpr decltype(auto) GetPair() {
return std::get<U_Pair<T_Key>>(m_map);
}
template < typename T_Key >
decltype(auto) GetValue() {
return std::get<1>(GetPair<T_Key>());
}
};
}
//!
//! #class EventManager
//!
template < typename ... T_Events >
class EventManager {
template < typename T_Event >
using U_EventCallback = std::function<void(T_Event)>;
template < typename T_Event >
using U_ListenersArray = std::vector<U_EventCallback<T_Event>>;
private:
containers::TypedMap<U_ListenersArray> m_listenersMap;
public:
EventManager() = default;
~EventManager() = default;
public:
template < typename T_Event >
decltype(auto) GetListeners() {
return m_listenersMap.template GetValue<T_Event>();
}
};
struct event1 {};
struct event2 {};
using U_EventManager = EventManager<event1,event2>;
int main() {
U_EventManager test;
auto result = test.GetListeners<event1>();
}
Wandbox
UPDATE:
So the error was generated because of a typo... But still I'd like to understand how this error occured, to be able to understand it next time.
In file included from prog.cc:1: /opt/wandbox/clang-head/include/c++/v1/tuple:1018:5: error: static_assert failed due to requirement '!is_same<pair<CType<event1>, vector<function<void (event1)>, allocator<function<void (event1)> > >
>, pair<CType<event1>, vector<function<void (event1)>, allocator<function<void (event1)> > > > >::value' "type not in empty type list"
static_assert(!is_same<_T1, _T1>::value, "type not in empty type list");
^ ~~~~~~~~~~~~~~~~~~~~~~~~~ /opt/wandbox/clang-head/include/c++/v1/tuple:1025:14: note: in instantiation of template class 'std::__1::__find_detail::__find_exactly_one_checked<std::__1::pair<meta::CType<event1>, std::__1::vector<std::__1::function<void (event1)>, std::__1::allocator<std::__1::function<void (event1)> > > >>' requested here
: public __find_detail::__find_exactly_one_checked<_T1, _Args...> {
^ /opt/wandbox/clang-head/include/c++/v1/tuple:1032:23: note: in instantiation of template class 'std::__1::__find_exactly_one_t<std::__1::pair<meta::CType<event1>, std::__1::vector<std::__1::function<void (event1)>, std::__1::allocator<std::__1::function<void (event1)> > > >>' requested here
return _VSTD::get<__find_exactly_one_t<_T1, _Args...>::value>(__tup);
^ prog.cc:45:29: note: in instantiation of function template specialization 'std::__1::get<std::__1::pair<meta::CType<event1>, std::__1::vector<std::__1::function<void (event1)>, std::__1::allocator<std::__1::function<void (event1)> > > >>' requested here
return std::get<U_Pair<T_Key>>(m_map);
^ prog.cc:51:36: note: in instantiation of function template specialization 'containers::TypedMap<U_ListenersArray>::GetPair<event1>' requested here
return std::get<1>(GetPair<T_Key>());
^ prog.cc:79:40: note: in instantiation of function template specialization 'containers::TypedMap<U_ListenersArray>::GetValue<event1>' requested here
return m_listenersMap.template GetValue<T_Event>();
^ prog.cc:92:24: note: in instantiation of function template specialization 'EventManager<event1, event2>::GetListeners<event1>' requested here
auto result = test.GetListeners<event1>();
^ 1 error generated.
You forgot to provide the content for your containers:
template < typename ... T_Events >
class EventManager {
/* ... */
private:
containers::TypedMap<U_ListenersArray> m_listenersMap; // <-- this line, you forgot the type list.
/* ... */
};
The marked line should be:
containers::TypedMap<U_ListenersArray, T_Events...> m_listenersMap;
Not sure... but it seems to me you've forgotten to use T_Events... in EventManager.
I mean... instead of
containers::TypedMap<U_ListenersArray> m_listenersMap;
you should, I suppose, write
containers::TypedMap<U_ListenersArray, T_Events...> m_listenersMap;
I run f<5>() and get the error log shown below:
template<bool B, typename T>
using Enable_if = typename std::enable_if<B,T>::type;
template<int N>
Enable_if<(N>0),void> f(){
std::cout << N;
f<N-1>();
}
template<int N>
Enable_if<(N==0),void> f(){
return;
}
In instantiation of 'Enable_if<(N > 0), void> f() [with int N = 1; Enable_if<(N > 0), void> = void]':
13:12: recursively required from 'Enable_if<(N > 0), void> f() [with int N = 3; Enable_if<(N > 0), void> = void]'
13:12: required from 'Enable_if<(N > 0), void> f() [with int N = 4; Enable_if<(N > 0), void> = void]'
25:8: required from here
13:12: error: no matching function for call to 'f()'
13:12: note: candidate is:
11:23: note: template<int N> Enable_if<(N > 0), void> f()
11:23: note: template argument deduction/substitution failed:
The code below runs fine with f<5>() and prints 543210:
template<int N>
void f(){
std::cout << N;
f<N-1>();
}
template<>
void f<0>(){
std::cout << 0;
}
It is taken from Stroustrup(C++11) page 845.
The 1st case is function template overloading. When f<1>() is called, the 1st overload will be invoked; inside which f<0>() is invoked. At that point, no matching function could be found; the 1st overload only instantiate when N > 0, the 2nd overload is declared afterwards then can't be found yet.
If you move the 2nd overload before the 1st one the code would work. LIVE
The 2nd case is function template with full template specialization. For f<1>(), the primary template will be invoked; inside which f<0>() is invoked. Then name lookup will still find the primary template itself; after that the specializations are checked then for f<0>() the specialization version will be called. That means if there's no specialization for N == 0 it will cause recursive template instantiation. LIVE
The first thing is that when instantiated with T == 1 the first template function in try to instantiate f<0>, however at that point a second template function is not yet declared so compiler only tries instantiated first template function and stops compilation after fails to do so. To fix this you need to add declaration of second template function prior to its use if first template function (Also Enable_if is not necessary because there is std::enable_if_t available in standard library that does exactly the same thing):
#include <iostream>
#include <type_traits>
// Forwrad declaration.
template<int N> ::std::enable_if_t<(0 == N)>
f(void);
template<int N> ::std::enable_if_t<(0 < N)>
f(void)
{
::std::cout << N;
f<N-1>();
}
template<int N> ::std::enable_if_t<(0 == N)>
f(void)
{
::std::cout << "0, end";
}
int
main()
{
f<5>();
::std::cout.flush();
return(0);
}
Run this code in online compiler
I'm trying to do the following (only relevant parts of code below):
template<typename ContainerType>
struct IsContainerCheck : is_container<ContainerType>
{
static constexpr char* err_value = "Type is not a container model";
};
namespace _check_concept {
template<typename ResultType>
struct run {
constexpr static int apply() {
static_assert(false, IsContainerCheck<ResultType>::err_value)
return 0;
}
};
template<>
struct run<true_t> {
constexpr static int apply() {
return 0;
}
};
}
This fails because the static_assert allows only literals to be printed. The same is with BOOST_STATIC_ASSERT_MSG macro.
So my question is - is there any way to output a constexpr string during compilation?
If there is a gcc extension providing this functionality that would also be great.
Used compiler gcc 4.8.1
GCC does not provide such a mechanism as you want. However you will not need
it if you are able to refactor your code somewhat as illustrated in the
following program. (I have filled in a few gaps so as to give us a
compilable example):
#include <type_traits>
#include <vector>
template<typename ContainerType>
struct is_container
{
static bool const value = false;
};
template<>
struct is_container<std::vector<int>>
{
static bool const value = true;
};
template<typename ContainerType>
struct IsContainerCheck // : is_container<ContainerType> <- Uneccessary
{
static_assert(is_container<ContainerType>::value,
"Type is not a container model");
};
namespace _check_concept {
template<typename ResultType>
struct run {
constexpr static int apply() {
return (IsContainerCheck<ResultType>(),0);
}
};
// No such specialization is necessary. Delete it.
// template<>
// struct run<true_t> {
// constexpr static int apply() {
// return 0;
// }
//};
}
using namespace _check_concept;
int main(int argc, char **argv)
{
auto verdict0 = run<std::vector<int>>::apply();
(void)verdict0;
// The following line will static_assert: "Type is not a container model"
auto verdict1 = run<float>::apply();
(void)verdict1;
return 0;
}
In your specialization _check_concept::struct run<true_t> I presume that
true_t is not an alias or equivalent of std::true_type, but rather
just a place-holder for some ResultType that is a container type. As
the test program shows, no such specialization is now necessary, because
IsContainerCheck<ResultType>() will static_assert, or not, depending
on ResultType, in the unspecialized run<ResultType>::apply().
I had some time (and a good liqueur to come along with it) to think more about the problem. This is what I came up with:
namespace _details {
struct PassedCheck {
constexpr static int printError () {
return 0; //no error concept check passed
}
};
template<template<typename> class ConceptCheck, typename ...ModelTypes>
struct check_concept_impl;
template<template<typename> class ConceptCheck, typename FirstType, typename ...ModelTypes>
struct check_concept_impl<ConceptCheck, FirstType, ModelTypes...> : mpl::eval_if< typename ConceptCheck<FirstType>::type,
check_concept_impl<ConceptCheck, ModelTypes...>,
mpl::identity<ConceptCheck<FirstType>>>
{ };
template<template<typename> class ConceptCheck, typename LastType>
struct check_concept_impl<ConceptCheck, LastType> : mpl::eval_if<typename ConceptCheck<LastType>::type,
mpl::identity<PassedCheck>,
mpl::identity<ConceptCheck<LastType>>>
{ };
}
template<template<typename> class ConceptCheck, typename ...ModelTypes>
struct check_concept {
private:
typedef typename _details::check_concept_impl<ConceptCheck, ModelTypes...>::type result_type;
public:
// the constexpr method assert produces shorter, fixed depth (2) error messages than a nesting assert in the trait solution
// the error message is not trahsed with the stack of variadic template recursion
constexpr static int apply() {
return result_type::printError();
}
};
template<typename ContainerType>
struct IsContainerCheck : is_container<ContainerType>
{
template<typename BoolType = false_t>
constexpr static int printError () {
static_assert(BoolType::value, "Type is not a container model");
return 0;
}
};
and the usage:
check_concept<IsContainerCheck, std::vector<int>, std::vector<int>, float, int>::apply();
The solution is probably not the most elegant one but I it keeps the assert message short:
In file included from ../main.cpp:4:0:
../constraint.check.hpp: In instantiation of ‘static constexpr int IsContainerCheck::printError() [with BoolType = std::integral_constant; ContainerType = float]’:
../constraint.check.hpp:61:34: required from ‘static constexpr int check_concept::apply() [with ConceptCheck = IsContainerCheck; ModelTypes = {std::vector >, std::vector >, float, int}]’
../main.cpp:25:83: required from here
../constraint.check.hpp:74:3: error: static assertion failed: Type is not a container model
static_assert(BoolType::value, "Type is not a container model");
The assert is issued in a constexpr method after the check_concept template specialization has been done. Embedding the static assert directly into the template class definition would drag the whole check_concept_impl recursion stack into the error message.
So changing the IsContainerCheck trait to something like (rest of the changes omitted for readibility):
template<typename ContainerType>
struct IsContainerCheck
{
static_assert(is_container<ContainerType>::type::value, "Type is not a container model");
};
would yield an error
../constraint.check.hpp: In instantiation of ‘struct IsContainerCheck’:
../constraint.check.hpp:36:9: required from ‘struct _details::check_concept_impl’
/usr/include/boost/mpl/eval_if.hpp:38:31: required from ‘struct boost::mpl::eval_if, _details::check_concept_impl, boost::mpl::identity > > >’
../constraint.check.hpp:36:9: required from ‘struct _details::check_concept_impl >, float, int>’
/usr/include/boost/mpl/eval_if.hpp:38:31: required from ‘struct boost::mpl::eval_if, _details::check_concept_impl >, float, int>, boost::mpl::identity > > >’
../constraint.check.hpp:36:9: required from ‘struct _details::check_concept_impl >, std::vector >, float, int>’
../constraint.check.hpp:53:84: required from ‘struct check_concept >, std::vector >, float, int>’
../main.cpp:25:81: required from here
../constraint.check.hpp:72:2: error: static assertion failed: Type is not a container model
static_assert(is_container::type::value, "Type is not a container model");
As you can see each recursive eval_if call is emended in the error description which is bad because it makes the error message dependent from the amount and type of template parameters.
I have some generic code which implements Pareto rule. It seems like well-formed code.
GCC 4.4 compiler messages about errors for newResult.set<Criterion>( criterion() ); expression. But I can't found problem.
Full error log:
trunk$ g++ -std=c++0x -o test test.cpp
t6.cpp: In member function ‘bool Pareto<Minimize<T>, Types ...>::operator()(Map&, Map&)’:
t6.cpp:24: error: expected primary-expression before ‘>’ token
t6.cpp:26: error: expected primary-expression before ‘>’ token
t6.cpp:26: error: expected primary-expression before ‘)’ token
t6.cpp:26: error: expected primary-expression before ‘>’ token
t6.cpp:26: error: expected primary-expression before ‘)’ token
t6.cpp: In member function ‘bool Pareto<Maximize<T>, Types ...>::operator()(Map&, Map&)’:
t6.cpp:43: error: expected primary-expression before ‘>’ token
t6.cpp:45: error: expected primary-expression before ‘>’ token
t6.cpp:45: error: expected primary-expression before ‘)’ token
t6.cpp:45: error: expected primary-expression before ‘>’ token
t6.cpp:45: error: expected primary-expression before ‘)’ token
Full code listing:
// TypeMap
template < typename ... Tail >
struct Holder;
template <typename ValueType, typename Head, typename ... Tail >
struct Holder<ValueType, Head, Tail ... > :
public Holder<ValueType, Head>,
public Holder<ValueType, Tail ... >
{};
template <typename ValueType, typename Head >
struct Holder<ValueType, Head>
{
ValueType value;
};
template < typename ... Types >
struct TypeMap;
template <typename ValueType, typename ... Types >
struct TypeMap<ValueType, Types ... > :
public Holder<ValueType, Types ... >
{
template <typename T>
void set(const ValueType& value)
{
((Holder<ValueType, T>*)this)->value = value;
}
template <typename T>
ValueType get()
{
return ((Holder<ValueType, T>*)this)->value;
}
};
// Objectives
template <typename Criterion> struct Maximize : public Criterion {};
template <typename Criterion> struct Minimize : public Criterion {};
// Criteria
struct Criterion1{ double operator()(){ return 0; }};
struct Criterion2{ double operator()(){ return 0; }};
// Pareto rule
template < typename ... Types > struct Pareto;
template < typename T, typename ... Types >
struct Pareto<Minimize<T>, Types ... >
{
template< typename Map >
bool operator()(Map& oldResult, Map& newResult)
{
typedef Minimize<T> Criterion;
Criterion criterion;
// ERROR HERE !!!
newResult.set<Criterion>( criterion() );
if(newResult.get<Criterion>() >= oldResult.get<Criterion>())
return false;
Pareto<Types ... > pareto;
return pareto(oldResult, newResult);
}
};
template < typename T, typename ... Types >
struct Pareto<Maximize<T>, Types ... >
{
template< typename Map >
bool operator()(Map& oldResult, Map& newResult)
{
typedef Maximize<T> Criterion;
Criterion criterion;
// ERROR HERE !!!
newResult.set<Criterion>( criterion() );
if(newResult.get<Criterion>() <= oldResult.get<Criterion>())
return false;
Pareto<Types ... > pareto;
return pareto(oldResult, newResult);
}
};
template<>
struct Pareto<>
{
template<typename Map>
bool operator()(Map& oldResult, Map& newResult)
{
oldResult = newResult;
return true;
}
};
int main()
{
TypeMap<double, Minimize<Criterion1>, Maximize<Criterion2>> oldResult, newResult;
Pareto<Minimize<Criterion1>, Maximize<Criterion2>> pareto;
pareto(oldResult, newResult);
}
Found it:
newResult.template set<Criterion>( criterion() );
if(newResult.template get<Criterion>() >= oldResult.template get<Criterion>())
return false;
You have to qualify the member function templates for the compiler in this case.
The lexer wouldn't be able to decide (at the time of template declaration, not instantiation) whether <Criterion means the start of a template parameter list or, instead, a comparison operator.
See
Using the template keyword as qualifier
What is the .template and ::template syntax about? (Comeau)
Standard, § 14.2, sub 4. and 5., noteworthy:
[ Note: As is the case with the typename prefix, the template prefix is allowed in cases where it is
not strictly necessary; i.e., when the nested-name-specifier or the expression on the left of the -> or . is not
dependent on a template-parameter, or the use does not appear in the scope of a template. —end note ]