在类模板的可变参数函数模板上使用decltype定义成员变量

时间:2015-09-30 19:11:54

标签: c++ templates c++14 variadic-templates

我试图在可变参数函数模板上使用'decltype'来获取其返回值类型,然后使用它来定义成员变量。但我一直收到这个错误:

D:\Qt Projects\OpenGL_PhysicsSim\lib\Physics Effects\include\ParticleList.hpp:89: error: cannot convert 'std::tuple<std::uniform_real_distribution<double>, std::uniform_real_distribution<double>, std::uniform_real_distribution<double> >' to 'int' in assignment
     distributionTuple = createDistribution<Type, Types...>(meanValue, meanValues..., varianceValue, varianceValues...);

基本上,decltype失败并将distributionTuple声明为int而不是推导返回类型createDistribution

template<typename AttributeType, typename Type, typename ...Types>
class ParticleAttributeGenerator
{
private:
 template<typename T>
 auto createDistribution(T meanValue, T varianceValue)
 {
   static_assert(
     std::is_integral<Type>::value || std::is_floating_point<Type>::value,
     "Type should be either integral value or floating point value");

   using distType = typename std::conditional< std::is_integral<T>::value,
                                               std::uniform_int_distribution<>,
                                               std::uniform_real_distribution<> >::type;
   T a = meanValue - varianceValue;
   T b = meanValue + varianceValue;

   return std::tuple<distType>(distType(a,b));
 }

 template<typename Tfirst, typename ...Trest>
 auto createDistribution(Tfirst meanValue, Trest... meanValues, Tfirst varianceValue, Trest... varianceValues)
 {
   static_assert(
     std::is_integral<Type>::value || std::is_floating_point<Type>::value,
     "Type should be either integral value or floating point value");

   using distType = typename std::conditional< std::is_integral<Tfirst>::value,
                                               std::uniform_int_distribution<>,
                                               std::uniform_real_distribution<> >::type;
   Tfirst a = meanValue - varianceValue;
   Tfirst b = meanValue + varianceValue;

   static_assert((sizeof...(meanValues)) == (sizeof...(varianceValues)), "number of meanValues and varianceValues should match!");

   return std::tuple_cat(std::tuple<distType>(distType(a,b)), createDistribution<Trest...>(meanValues..., varianceValues...));
 }

public:
 ParticleAttributeGenerator(Type meanValue, Types... meanValues, Type varianceValue, Types... varianceValues)
 {
   distributionTuple = createDistribution<Type, Types...>(meanValue, meanValues..., varianceValue, varianceValues...);  // 89 : error          
 }

private:
  //using result_type_t = typename std::result_of<createDistribution(Type, Types..., Type, Types...)>::type;
  decltype (createDistribution<Type, Types...>(Type, Types..., Type, Types...)) distributionTuple;
  //decltype (createDistribution<Type, Types...>(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)) distributionTuple;
};

当我提供createDistribution的所有参数值时,它会起作用,但这不是我正在寻找的行为。由于我不知道该函数将有多少参数,因此它必须保留为可变参数模板函数。

我打算如何使用类模板的示例:

ParticleAttributeGenerator<glm::vec3, float, float, float> example1(3.0f, 1.0f, 3.0f, 1.0f, 3.0f, 1.0f);
ParticleAttributeGenerator<glm::u8vec4, uint8_t, uint8_t, uint8_t, uint8_t> example2(3, 2, 25, 5, 51, 12, 32, 3);

如果我没有任何成员变量并使用distributedTuple

auto distributionTuple = createDistribution<Type, Types...>(meanValue, meanValues..., varianceValue, varianceValues...);

它编译,因此我相信createDistribution设法递归地定义元组。但这对我没用。一定有一些我错过的错误。 我在-std = c ++ 14模式下使用GCC 4.9.2。

2 个答案:

答案 0 :(得分:4)

首先,让我们将示例大规模简化为更易于管理的内容:

template <typename T>
class Bar
{
private:
    template <typename U>
    auto foo(U a, U b)
    {
        return std::tuple<U>(a+b);
    }

public:
    Bar(T a, T b)
    {
        distributionTuple = foo<T>(a, b);
    }

private:
    decltype (foo<T>(T, T)) distributionTuple;
};

int main()
{
    Bar<int> b(4, 4);
}

这为我提供了您在问题中提供的示例编译错误(无法将std::tuple<int>转换为int。这是因为:

foo<T>(T, T)

不是有效的函数调用。您需要使用具有这些类型的表达式来调用foo<T>。不仅仅是一个类型列表。为此,我们有std::declval。那就是:

foo<T>(std::declval<T>(), std::declval<T>())

进行更改后,您将收到一个新的编译错误:“无法在没有对象的情况下调用成员函数foo。”那么让我们再次添加declval的对象。以下编译:

template <typename T>
class Bar
{
private:
    template <typename U>
    auto foo(U a, U b)
    {
        return std::tuple<U>(a+b);
    }

public:
    Bar(T a, T b)
    {
        distributionTuple = foo<T>(a, b);
    }

private:
    decltype(std::declval<Bar>().foo<T>(
                 std::declval<T>(), 
                 std::declval<T>())
             ) distributionTuple;
};

答案 1 :(得分:1)

当您需要在未评估的上下文中构建函数的完整调用时,请使用std::declval。使用它,您可以像这样声明distributionTuple

decltype (std::declval<ParticleAttributeGenerator>().createDistribution<Type, Types...>(std::declval<Type>(), std::declval<Types>()..., std::declval<Type>(), std::declval<Types>()...)) distributionTuple;