我有以下代码不能在Visual C++ 2015下编译,但在GCC 4.8.4下编译.我想知道哪个是对的?有问题的代码如下:
template <class T> class ATemplate;
template <class R, class A1>
struct ATemplate<R(A1)>{ };
int main()
{
ATemplate<void(int)> x;
// ATemplate<void(int)override> y; //---Does not compile!!!
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在下面(或const)使用override作为说明符是错误的.GMock库中存在类似的代码,其中宏扩展用于生成模板参数(包括覆盖)以及实际的函数签名.
删除注释掉的行时,Visual C++ 2015会产生以下错误:
x.cpp(11): error C2062: type 'int' unexpected
x.cpp(11): error C2976: 'ATemplate': too few template arguments
x.cpp(4): note: see declaration of 'ATemplate'
x.cpp(11): error C2079: 'y' uses undefined class 'ATemplate'
Run Code Online (Sandbox Code Playgroud)
下面的答案之一提到覆盖在自由函数的上下文中是没有意义的(有效点) - 这是否意味着GCC在这里是错误的.在这种情况下,const说明符也没有意义(对于自由函数),但仍允许(通过VC++)??? 此外,它提到虚拟说明符应仅存在于声明中 - 这与此情况没有区别(因为不存在定义).对于virtual关键字,可以在派生中省略,因为它对代码是否编译没有区别,但对于覆盖情况,它不是好的,因为它会产生很大的不同.
当使用ReturnType(ArgType arg)...可能的const或override说明符作为宏参数(如GMock那样)时,VCC施加的限制导致此代码无法编译(显然也是Clang的情况).哪个是对的?
标准没有说明在这种情况下(模板参数的上下文?)不应该使用覆盖说明符,是吗?
我有以下代码(涉及多个文件)...
//--- SomeInterface.h
struct SomeInterface
{
virtual void foo() = 0;
virtual ~SomeInterface(){}
};
//--- SomeInterfaceUser.h
#include <memory> //shared_ptr
class SomeInterface;
//NOTE: struct SomeInterface... causes linker error to go away...
class SomeInterfaceUser
{
public:
explicit SomeInterfaceUser(std::shared_ptr<SomeInterface> s);
};
//SomeInterfaceUser.cpp
#include "SomeInterfaceUser.h"
#include "SomeInterface.h"
SomeInterfaceUser::SomeInterfaceUser(std::shared_ptr<SomeInterface> s)
{
}
//SomerInterfaceUserInstantiator.cpp
#include "SomeInterfaceUser.h"
#include "SomeInterfaceImpl.h"
struct SomeInterfaceImpl : SomeInterface
{
virtual void foo(){}
};
void test()
{
SomeInterfaceUser x{std::make_shared<SomeInterfaceImpl>()};
}
Run Code Online (Sandbox Code Playgroud)
使用Visual C ++编译器,我得到一个链接器错误(LNK2019)。使用GCC 4.8.4并非如此。将前向声明类SomeInterface更改为struct SomeInterface可使链接器错误消失。我一直以为应该可以互换使用类/结构?SomeInterfaceUser的接口不应取决于SomeInterface是定义为类还是struct,不是吗?
这是Visual C ++错误吗?我找不到任何与此有关的东西。我怀疑将struct用作模板参数这一事实与它有关。
感谢您的帮助。
我有以下代码令我感到惊讶(使用libstdc ++ 4.8)...
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
std::string s("some content");
std::stringstream ss(s, std::ios::in|std::ios::ate);
std::istream& file = ss;
//ss.clear(); Makes no difference...
std::cout << "tellg() pos: " << file.tellg() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
...具有以下输出.
tellg()pos:0
此行为与使用std :: ifstream(std :: ios :: ate)时不同.
鉴于下面的示例,我很惊讶地发现尽管显式删除了默认构造函数(或者默认为此),但仍然可以进行聚合初始化.
#include <iostream>
struct DefaultPrivate
{
const int n_;
static const DefaultPrivate& create();
private:
DefaultPrivate() = delete;
};
const DefaultPrivate& DefaultPrivate::create()
{
static DefaultPrivate result{10};
return result;
}
int main() {
DefaultPrivate x; //Fails
DefaultPrivate y{10};//Works
return 0;
}
Run Code Online (Sandbox Code Playgroud)
私有默认(或删除)构造与聚合初始化之间的关系是否未在标准中指定?
GCC 6.3和VCC 2017就属于这种情况
我问这个问题的原因是,我希望更改对默认构造函数的访问会阻止公共聚合初始化
c++ initialization language-lawyer aggregate-initialization c++11
这些建议boost :: pointer_container 建议从boost :: noncopyable派生ABC,以防止在层次结构中进一步切片.除了上面提到的优点外,我有理由不遵循这项建议吗?
我在下面有一个小例子,它有两个返回std :: function的函数(getFoo1 ... 2).
我关心lambda行为(我预计它不会编译,与getFoo2一样).任何见解都会受到欢迎.
注意:编译器使用GCC 4.8.*.
#include <iostream>
#include <functional>
struct X
{
void foo(){ std::cout << " non const !!!! foo called" << std::endl;}
std::function<void()> getFoo1() const
{
//Compiles, even though "non const" this required...
return [this]{foo();};
}
std::function<void()> getFoo2() const
{
//Fails to compiler due to non const this required
return std::bind(&X::foo, this);
}
};
int main()
{
X().getFoo1()();
X().getFoo2()();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
亲切的问候,
维尔纳
在Unix/Linux环境中,很容易确定程序是否由于信号中断(sigterm)而退出,以及是哪个信号中断。
可以确定(以编程方式使用 API 或退出代码)Windows 程序是否因信号(sigkill,以 powershell 为例)而退出?
Windows 有“很多很多”错误代码,我想知道哪些错误代码仅与进程管理相关。
亲切的问候,
c++ ×7
c++11 ×4
boost ×1
c ×1
compiler-bug ×1
iostream ×1
libstdc++ ×1
powershell ×1
visual-c++ ×1
winapi ×1