Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数模板的类型定义(部分实例化)_C++_Templates_C++11_Tuples_Template Meta Programming - Fatal编程技术网

C++ 函数模板的类型定义(部分实例化)

C++ 函数模板的类型定义(部分实例化),c++,templates,c++11,tuples,template-meta-programming,C++,Templates,C++11,Tuples,Template Meta Programming,对不起,我真的不知道怎么称呼我的问题,希望它适合 我有一个函数模板,它获取一个参数作为模板参数。对于该参数,我需要另一个模板参数来声明该参数的类型,但在稍后调用该函数时,我想省略该参数的类型。因此,我希望有某种类型的typedef(或其他机制)来摆脱它 我在其他模板中看到过类似的机制,例如 // given: rule is a template with three arguments template<typename Attr> using Rule = rule<It,

对不起,我真的不知道怎么称呼我的问题,希望它适合

我有一个函数模板,它获取一个参数作为模板参数。对于该参数,我需要另一个模板参数来声明该参数的类型,但在稍后调用该函数时,我想省略该参数的类型。因此,我希望有某种类型的typedef(或其他机制)来摆脱它

我在其他模板中看到过类似的机制,例如

// given: rule is a template with three arguments
template<typename Attr> using Rule = rule<It, Attr(), Skipper>;
下面是函数,它用于访问带有枚举的元组(比较:)

模板
typename std::元组元素::类型&
get(std::tuple&t){
返回std::get(t);
}
由于它将与几个枚举一起使用,我不想像他那样在模板中硬连接枚举

它被称为:(1)


std::cout我认为您最好能为每个枚举创建一个
get()
函数。以下是一个例子:

#include <tuple>
#include <string>
#include <iostream>

typedef std::tuple<std::string,std::string> Tuple1;
typedef std::tuple<std::string,int> Tuple2;

enum class Enum1 {
  name,
  address
};

enum class Enum2 {
  state,
  zip_code
};

template <typename Enum>
constexpr std::size_t indexOf(Enum value)
{
  return static_cast<std::size_t>(value);
}

template <typename Enum,Enum enum_value,typename Tuple>
constexpr auto get(const Tuple &value)
  -> decltype(std::get<indexOf(enum_value)>(value))
{
  return std::get<indexOf(enum_value)>(value);
}

template <Enum1 enum_value>
constexpr auto get(const Tuple1 &value)
  -> decltype(get<Enum1,enum_value>(value))
{
  return get<Enum1,enum_value>(value);
}

template <Enum2 enum_value>
constexpr auto get(const Tuple2 &value)
  -> decltype(get<Enum2,enum_value>(value))
{
  return get<Enum2,enum_value>(value);
}

int main(int,char**)
{
  Tuple1 a("John","123 Foo St");
  Tuple2 b("California",90210);
  std::cerr << get<Enum1::name>(a) << "\n";
  std::cerr << get<Enum1::address>(a) << "\n";
  std::cerr << get<Enum2::state>(b) << "\n";
  std::cerr << get<Enum2::zip_code>(b) << "\n";
}
#包括
#包括
#包括
typedef std::tuple Tuple1;
typedef std::tuple Tuple2;
枚举类Enum1{
名称
地址
};
枚举类Enum2{
国家,,
邮政编码
};
模板
constexpr std::size\u t indexOf(枚举值)
{
返回静态_cast(值);
}
模板
constexpr自动获取(const元组和值)
->decltype(std::get(value))
{
返回std::get(值);
}
模板
constexpr自动获取(consttuple1和value)
->decltype(获取(值))
{
返回get(值);
}
模板
constexpr自动获取(consttuple2和value)
->decltype(获取(值))
{
返回get(值);
}
int main(int,char**)
{
Tuple1a(“约翰”,“富街123号”);
Tuple2b(“加利福尼亚”,90210);

可能会使用默认参数吗?这个问题可以简化吗?你能说一下你现在有什么(工作)以及你希望它是什么样子吗?这里所述的模板是有效的。它在(1)中被称为like,我觉得太冗长了。我想把它称为我在第一个问题中所说的。这是不可能的,原因与此类似:@VaughnCato:在使用时:“std::get(tuple);”它可以在不直接拼写枚举的情况下工作,因此有一种方法…感谢您的努力。我希望绕过枚举1和枚举2的显式实例化。我仍然不明白的是,为什么模板constexpr auto get(enum enum_value,const Tuple&value)->decltype(std::get(value))是不可能的(在函数体外部使用参数'enum_value'),但模板自动添加(T,U)->decltype(T+U);可以。@MikeM:在
模板自动添加(T,U)->decltype(T+U)的情况下
,函数的返回类型仅取决于编译时已知的参数类型。在
模板constexpr auto get(Enum Enum_value,const Tuple&value)->decltype(std::get(value))
的情况下,返回类型取决于参数的值,而该值在编译时通常是未知的。非常感谢。现在我看到了区别。因此,需要像constexpr这样的参数吗?我正试图了解底层规则。我刚刚发现,虽然indexOf工作正常,但这不起作用
template struct Test{Test(Enum value){…}
并调用它
Test(myEnum::type1)
嗯,还在学习。谢谢你的努力。
template<typename Enum, Enum enum, class ... Types>
typename std::tuple_element<static_cast<std::size_t>(enum), std::tuple<Types...> >::type&
get(std::tuple<Types...>& t) {
  return std::get<static_cast<std::size_t>(enum)>(t);
}
std::cout << get<myEnum, myEnum::type1>(tuple);
std::cout << new_get < myEnum::type1 > (tuple);
#include <tuple>
#include <string>
#include <iostream>

typedef std::tuple<std::string,std::string> Tuple1;
typedef std::tuple<std::string,int> Tuple2;

enum class Enum1 {
  name,
  address
};

enum class Enum2 {
  state,
  zip_code
};

template <typename Enum>
constexpr std::size_t indexOf(Enum value)
{
  return static_cast<std::size_t>(value);
}

template <typename Enum,Enum enum_value,typename Tuple>
constexpr auto get(const Tuple &value)
  -> decltype(std::get<indexOf(enum_value)>(value))
{
  return std::get<indexOf(enum_value)>(value);
}

template <Enum1 enum_value>
constexpr auto get(const Tuple1 &value)
  -> decltype(get<Enum1,enum_value>(value))
{
  return get<Enum1,enum_value>(value);
}

template <Enum2 enum_value>
constexpr auto get(const Tuple2 &value)
  -> decltype(get<Enum2,enum_value>(value))
{
  return get<Enum2,enum_value>(value);
}

int main(int,char**)
{
  Tuple1 a("John","123 Foo St");
  Tuple2 b("California",90210);
  std::cerr << get<Enum1::name>(a) << "\n";
  std::cerr << get<Enum1::address>(a) << "\n";
  std::cerr << get<Enum2::state>(b) << "\n";
  std::cerr << get<Enum2::zip_code>(b) << "\n";
}