我正在编写这样的模板函数:
template<typename T>
std::string EncodeData(int DataType, T Data, std::string ReadCommandID, std::string& ThisID);
Run Code Online (Sandbox Code Playgroud)
我想使用的类型T是一些不同的结构,例如:
struct A
{
int A_data;
bool A_data_2;
// etc.......
};
struct B
{
double B_data;
char B_data_2;
// etc.......
};
Run Code Online (Sandbox Code Playgroud)
我希望函数可以根据不同的struct传递来访问不同的成员变量T,所以我写了这样的代码:
template<typename T>
std::string EncodeData(int DataType, T Data, std::string ReadCommandID, std::string& ThisID)
{
if(std::is_same<T, A>{})
{
// Do something with Data.A_data and Data.A_data_2
}
else if(std::is_same<T, B>{})
{
// Do something with Data.B_data and Data.B_data_2
}
// other code.
}
Run Code Online (Sandbox Code Playgroud)
并使用它: …
我正在考虑编写一个既适用于序列容器又适用于关联容器的函数。这就像
template<class C, class V = typename C::key_type>
bool has_val(const C& c, const V& v)`
Run Code Online (Sandbox Code Playgroud)
在函数内部,我想
const_iterator find(key_type) const。std::find()像 之类的序列容器std::vector。检查的最佳做法是什么(1)?
如果我上面描述的不是最好的,请告知是否有更好的方法?
(不幸的是,我无法使用宏访问较新的 Folly FOLLY_create_member_invoker,但我确实有FOLLY_CREATE_HAS_MEMBER_FN_TRAITS。不过,我无法成功完成它)
我想传递任意容器作为函数的参数并迭代它(没有擦除也没有推送元素).不幸的是,似乎没有标准的方法来做到这一点.
我想到的第一个解决方案是一个接口(让我们称之为CollectionInterface),它将由包装STL容器的类实现.所以函数声明看起来像:
f(const CollectionInterface * collection);
Run Code Online (Sandbox Code Playgroud)
或者,我正在考虑方法模板,它具有在编译时保持绑定的优点:
template <class CONTAINER> void f(const CONTAINER & collection);
Run Code Online (Sandbox Code Playgroud)
你认为哪种方式更好?
我想创建一个函数模板,其中类T仅限于特殊基类T_base的派生类.实现这一目标的有效方法是什么?谢谢你的帮助!
假设我template<int I> void ft()在结构模板中有一个静态函数模板template<bool B> S,我想ft从另一个函数模板调用template<bool B> void g(),将bool模板参数传递g给S:
template<bool B>
struct S {
static void f() {
}
template<int I>
static void ft() {
}
};
template<bool B>
void g() {
S<B>::f();
S<B>::ft<12>();
}
int main() {
g<true>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在GCC 4.5.2中进行编译会给出关于该行的两个错误S<B>::ft<12>():
Comeau(http://www.comeaucomputing.com/tryitout/),在严格的C++ 03模式下,也抱怨该行,说明"预期的表达式",在右括号下面有一个插入符号.S<B>::f()然而,编译器都没有抱怨该行,而且Comeau实际上可以在轻松模式下编译整个最小示例.
如果我删除了g模板,而是像这样实例化S模板参数g:
void …Run Code Online (Sandbox Code Playgroud) 我仍在试图弄清楚模板.我已经阅读了有关专业化规则的内容,并且不明白这里发生了什么.
我在templates.h中定义了以下内容:
#include <iostream>
template <typename foo>
void f(foo p)
{
std::cout << "one" << std::endl;
}
template <typename bar>
void f(int p)
{
std::cout << "two" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我包括这个,并在我的主要像这样称呼它
f(1);
f("x");
Run Code Online (Sandbox Code Playgroud)
我明白了
one
one
Run Code Online (Sandbox Code Playgroud)
现在的问题是,为什么第一个更具体而不是第二个用于整数?我觉得它至少应该是模棱两可的,根本不起作用.
我在玩 C++ 模板类型推导并设法编译了这个小程序。
template<typename T>
void f(const T& val)
{
val = 1;
}
int main()
{
int i = 0;
f<int&>(i);
}
Run Code Online (Sandbox Code Playgroud)
它可以在所有主要编译器上编译,但我不明白为什么。为什么可以f赋值val,什么时候val显式标记const?它是这些编译器中的错误还是根据 C++ 标准的有效行为?
template <typename T>
bool validate(const T& minimum, const T& maximum, const T& testValue) {
return testValue >= minimum && testValue <= maximum;
}
template <>
bool validate<const char&>(
const char& minimum,
const char& maximum,
const char& testValue)
{
char a = toupper(testValue);
char b = toupper(minimum);
char c = toupper(maximum);
return a >= b && a <= c;
}
Run Code Online (Sandbox Code Playgroud)
这是函数模板,不知何故在调用函数main时validate,它const char&甚至在参数出现时都不会使用第二个函数(即函数)char.任何人都可以看到我的问题在哪里?
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void wrapper(T& u)
{
g(u);
}
class A {};
void g(const A& a) {}
int main()
{
const A ca;
wrapper(ca);
wrapper(A()); // Error
}
Run Code Online (Sandbox Code Playgroud)
嗨,我有一个问题,为什么最后一个语句给出编译器错误.
:18:10:错误:无法将类型为'A&'的非const左值引用绑定到类型'A'包装器(A())的右值;
我认为模板类型T将被推断为,const A&因为ca它也被推断为const A&.在这种情况下,为什么类型扣除失败?
在构建期间,编译器会不断分配不兼容的类型。
错误信息:
error: assigning to 'int' from incompatible type 'QString'
typeduserproperty.cpp:115:28: note: in instantiation of member function 'core::TypedUserProperty<int>::setValue' requested here
Run Code Online (Sandbox Code Playgroud)
样例代码
/**
* @brief setValue
* set value to property
* @param val
* value to set to property
* @return
* true - successfully set value
* false - invalid value
*/
template<class T>
void TypedUserProperty<T>::setValue(QVariant val)
{
if (std::is_same<T, int>::value == true)
{
this->_value = val.toInt();
}
else if (std::is_same<T, QString>::value == true)
{
this->_value = val.toString();
} …Run Code Online (Sandbox Code Playgroud) c++ templates if-statement function-templates template-classes
c++ ×10
templates ×7
c++17 ×2
base-class ×1
collections ×1
containers ×1
if-statement ×1
std ×1
stl ×1
struct ×1