我想在模板类中定义一些模板成员方法,如下所示:
template <typename T>
class CallSometing {
public:
void call (T tObj); // 1st
template <typename A>
void call (T tObj, A aObj); // 2nd
template <typename A>
template <typename B>
void call (T tObj, A aObj, B bObj); // 3rd
};
template <typename T> void
CallSometing<T>::call (T tObj) {
std::cout << tObj << ", " << std::endl;
}
template <typename T>
template <typename A> void
CallSometing<T>::call (T tObj, A aObj) {
std::cout << tObj << ", " << aObj …
Run Code Online (Sandbox Code Playgroud) c++ templates arguments compiler-errors template-meta-programming
class Child;
class Parent
{
public:
void (*funcPointer)();
void (*funcPointer2)(Parent* _this);
void (Child::*funcPointer3)();
};
class Child: public Parent
{
public:
void TestFunc(){
}
void Do(){
Parent p;
p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)'
p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)'
p.funcPointer3=TestFunc; //this works
p.funcPointer3=&Child::TestFunc; // this works too.
p.funcPointer3(); // error, term does not evaluate to a function taking 0 arguments …
Run Code Online (Sandbox Code Playgroud) c++ methods member-function-pointers function-pointers delayed-execution
即使C++ 11推出,我也听到了很多关于C++ 17和C++ 14的讨论.
但现在我明白C++ 17有望在2017年秋季推出(没有概念),但我仍然不确定以下标准是什么.
我听说C++ 21开始了.那是准确的吗?
所以我在这里问了一个问题:Lambda在最新的Visual Studio上运行,但是不起作用在我得到响应的其他地方,我的代码是实现定义的,因为标准的25.1 [algorithms.general] 10说:
除非另有说明,否则允许将函数对象作为参数的算法自由复制这些函数对象.对象标识很重要的程序员应该考虑使用指向非复制实现对象的包装类,例如
reference_wrapper<T>
我想知道为什么会这样?我们被告知我们整个生命中通过引用来获取对象,为什么标准是按值获取函数对象,更糟糕的是我的链接问题制作了这些对象的副本?这样做有什么我不明白的优点吗?
所以在c ++ 11中,Chrono Library提供了duration_cast
:
计算以可用的最宽类型完成,并且仅在完成时将其转换为结果类型,就像通过static_cast一样
返回ToDuration中可表示的最大持续时间t,该持续时间小于或等于d
因此,对于所有人来说x
,这两个调用的结果是相同的:
chrono::duration_cast<chrono::seconds>(x)
chrono::floor<chrono::seconds>(x)
c ++ 14提供了可变模板,它在visual-studio-2017中运行良好,但在lambdas中它们似乎分崩离析.例如:
template <typename T>
const auto PI = std::acos(static_cast<T>(-1));
int main() {
auto func = []() { cout << PI<float> << endl; };
func();
}
Run Code Online (Sandbox Code Playgroud)
在gcc 6.3上输出:
3.14159
在Visual Studio 2017上,此输出:
0.0
我需要得到一个QStringList
或一个包含所有QString
s 的数组QComboBox
.
我找不到QComboBox
这样做的方法,实际上我甚至找不到QAbstractItemModel
这样做的方法.
这真的是我唯一的选择:
std::vector< QString > list( myQComboBox.count() );
for( auto i = 0; i < list.size(); i++ )
{
list[i] = myQComboBox.itemText( i );
}
Run Code Online (Sandbox Code Playgroud) 一些标准iomanip
函数需要参数.
我想知道这是如何实现的,例如,我可以用函数做类似的事情吗?这真的是我需要这个答案的解决方案,但我无法弄清楚如何做到这一点.
当我查看setw
函数的定义时,例如在http://en.cppreference.com中,它将返回类型列为"未指定",它也只列出一个参数,而不是stream&
参数.这是如何运作的?
这个问题有一个很好的答案,但对于个人提出这个问题; 此答案仅在与以下功能结合使用时才有用ios_base
:
我上一家公司的主要工程师有一个规则,即private
static
方法应该作为实现文件中的函数实现,而不是作为类方法.
我不记得他的规则是否有任何例外.我在当前的工作中偶然发现了它的动机:如果有问题的函数的参数或返回类型是需要在标题中包含定义文件的对象,这可能会导致不必要的困难.
这足以让我远离private
static
再次使用某种方法,但在我把它们写下来之前,我想知道是否有人知道他们填充的利基,实现文件功能不会?
编辑:
这里的一个例子可能会有帮助.假设这是声明的开始,class Foo
其他方法将void foo()
在实现文件中调用:
class Foo {
static void foo();
Run Code Online (Sandbox Code Playgroud)
所以foo
只能通过其他方法访问Foo
.为什么我不在foo
实现文件中定义,并将它一起保留在标题之外?