我试图用C++编译一个简单的VS程序作为类的赋值.我们只包括<iostream>并且我一直收到此错误:
1> Assignment.cpp(15):致命错误C1010:查找预编译头时意外结束文件.你忘记添加
'#include "StdAfx.h"'到你的来源了吗?
我的程序确实很小......
#include <iostream>
using namespace std;
int main()
{
unsigned int day = 30;
cout << "My Name is John Doe" << endl;
cout << "My Major is CS" << endl;
cout << "I was born on day " << day << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我刚刚安装了Visual Studio Express 2010.我真的很想开始一个空的项目,而不是安装所有这些预定义的文件,我认为它会使它变得更容易但我在创建项目时从未得到过这个选项.有人有什么建议吗?
如果您需要一个计数变量,肯定必须有一个整数必须支持的上限和下限.那么为什么不通过选择适当的(u)int_fastxx_t数据类型来指定这些限制呢?
请考虑以下代码:
template<typename>
struct One {};
template<typename, typename>
struct Two {};
template<template<typename...> class TTP, typename...>
struct SS;
#ifdef TEST_TTP
template<template<typename> class OneParam,
typename... Ts>
struct SS<OneParam, Ts...> {};
template<template<typename, typename> class TwoParam,
typename... Ts>
struct SS<TwoParam, Ts...> {};
#else // TEST_TTP
template<template<typename> class OneParam,
typename TParam>
struct SS<OneParam, TParam> {};
template<template<typename, typename> class TwoParam,
typename TParam1,
typename TParam2>
struct SS<TwoParam, TParam1, TParam2> {};
#endif // TEST_TTP
int main() {
SS<One, int> ssoi;
SS<Two, int, int> sstii;
}
Run Code Online (Sandbox Code Playgroud)
如果TEST_TTP未定义,此代码将在Clang,GCC和MSVC上正确编译.但是,如果它被 …
目前,我们有两个主要的编译时评估选项:模板元编程(通常使用模板结构和/或变量)和constexpr操作1。
template<int l, int r> struct sum_ { enum { value = l + r }; }; // With struct.
template<int l, int r> const int sum = sum_<l, r>::value; // With struct & var.
template<int l, int r> const int sub = l - r; // With var.
constexpr int mul(int l, int r) { return l * r; } // With constexpr.
Run Code Online (Sandbox Code Playgroud)
其中,我们保证可以在编译时对所有这四个值进行评估。
template<int> struct CompileTimeEvaluable {};
CompileTimeEvaluable<sum_<2, 2>::value> template_struct; // Valid.
CompileTimeEvaluable<sum<2, 2>> …Run Code Online (Sandbox Code Playgroud) 我是运算符重载试验new和delete,并注意到MSVC和GCC出现在他们的实施而不同operator delete.请考虑以下代码:
#include <cstddef>
struct CL {
// The bool does nothing, other than making these placement overloads.
void* operator new(size_t s, bool b = true);
void operator delete(void* o, bool b = true);
};
// Functions are simple wrappers for the normal operators.
void* CL::operator new(size_t s, bool b) { return ::operator new(s); }
void CL::operator delete(void* o, bool b) { return ::operator delete(o); }
auto aut = new (false) CL;
Run Code Online (Sandbox Code Playgroud)
此代码将使用GCC(使用Ideone和TutorialsPoint在线编译器进行测试)进行正确编译,但不能使用MSVC(使用MSVS 2010,MSVS …
我有以下问题。我有一些使用 Eigen3 的代码。Eigen3 使用 int 或 long int 作为索引。在代码中的某些地方,我必须将特征数组中的值存储在std::vector.
这是一些例子:
std::vector myStdVector;
Eigen::VectorXd myEigen;
....
for(size_t i=0; i<myStdVector.size(); i++)
{
myStdVector[i] = myEigen(i);
}
Run Code Online (Sandbox Code Playgroud)
这里我得到编译器警告:
警告:隐式转换会丢失整数精度:“const size_t”(又名“const unsigned long”)到“int”
所以我当然可以static_cast<int>(i)在发生这种情况的所有函数中添加 a ,但我想知道是否有更好的方法来处理这种情况。我想许多其他“库混合”也会发生这种情况。
我想知道关于派生类构造函数的委派。当您还必须调用父类的构造函数时,委托构造函数的正确方法是什么?我知道您不能在同一初始化列表中同时包含委托和成员初始化,但是我不知道调用父类的构造函数是否具有相同的限制。
// Option 1: Call parent class constructor, then delegate:
class Foo {
public:
Foo(int);
};
class Bar : public Foo {
public:
Bar(int, float) : Foo(int), Bar(int, float, 'c');
Bar(int, float, char);
};
// Option 2: Delegate, then call parent class constructor:
class Foo {
public:
Foo(int);
};
class Bar : public Foo {
public:
Bar(int, float) : Bar(int, float, 'c'), Foo(int);
Bar(int, float, char);
};
// Option 3: Primary constructor calls parent class constructor:
class Foo {
public: …Run Code Online (Sandbox Code Playgroud) 我正在尝试函子,无意中发现了一些decltype我觉得有趣的事情:据我所知,没有办法使用它来生成指向重载函数的函数指针,或为其提供解决所述重载所需的信息当尝试这样做时。
// Simple test code.
// Please ignore the possibility of attempting to dereference a nullptr function pointer,
// proper checking was omitted for brevity.
template<typename... Ts> struct Functor;
template<typename F, typename R, typename... Ts>
struct Functor<F, R(*)(Ts...)> {
Functor() : func(nullptr) { }
Functor(F f) : func(f) { }
R operator()(F f, Ts... ts) { return 2 * f(ts...); }
R operator()(Ts... ts) { return 2 * func(ts...); }
private:
F func;
};
template<typename F>
decltype(auto) functorReturner(F …Run Code Online (Sandbox Code Playgroud) c++ ×8
c++11 ×3
g++ ×2
visual-c++ ×2
clang++ ×1
constexpr ×1
constructor ×1
decltype ×1
eigen3 ×1
int ×1