好吧,我会发布完整的程序,即使它有无关的东西,而且有问题的代码是死代码......
#include <iostream>
#include <fstream>
namespace detail {
// Solution by Johannes Schaub alias litb
// http://groups.google.com/group/comp.std.c++/browse_thread/thread/b567617bfccabcad
template<int> struct D {};
typedef char yes[1];
typedef char no[2];
template< class T, class U >
yes& f( int, D< sizeof T(*(U*)0) >* = 0 );
template< class T, class U >
no& f( ... );
template< class To, class From >
struct IsExplicitlyConvertible
{
enum{ yes = (sizeof detail::f< To, From >(0) == sizeof( detail::yes ) ) };
};
bool const streamsSupportWindows …Run Code Online (Sandbox Code Playgroud) 码:
#include <memory>
using namespace std;
struct T {};
T* foo() { return new T; }
T const* bar() { return foo(); }
int main()
{
unique_ptr< T const > p1( bar() ); // OK
unique_ptr< T const [] > a1( bar() ); // OK
unique_ptr< T const > p2( foo() ); // OK
unique_ptr< T const [] > a2( foo() ); // ? this is line #15
}
Run Code Online (Sandbox Code Playgroud)
Visual C++ 10.0和MinGW g ++ 4.4.1的示例错误:
[d:\dev\test] > cl foo.cpp foo.cpp …
我最近再次遇到了符号
( const int[10] ){ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }
Run Code Online (Sandbox Code Playgroud)
我记得它在C和C++中都是允许的,但是通过完全不同的语言机制.
我相信在C++中,正式视图是通过一个epxlicit类型转换(T) cast-expression构建一个未命名的临时表达式,它将减少为a static_cast,通过C++11§5.2.9/ 4构造一个对象:
"表达
e可以显式转换到类型T使用static_cast的形式的static_cast<T>(e),如果声明T t(e);是良好的形成,对于某些发明临时变量t(8.5)
但是,Cast-expression语法由C++11§5.4/ 2定义为一元表达式,或者递归地,一个( type-id 强制转换) 表达式,其中单个基本情况是简化为一元表达式.
据我所知,一个支撑的init-list不是表达式?
另一种观点可能是它是通过功能符号进行的显式类型转换,C++11§5.2.3/ 3,
"一个simple-type-specifier或typename-specifier后跟一个braced-init-list创建一个指定类型的临时对象
但据我所知,简单类型说明符不能涉及括号,而typename-specifier涉及关键字typename?
我刚下载了CLang源代码,使用CMake创建了一个Visual C++ 10 IDE工作区,并构建了Visual C++ 10.0(express)中的所有内容.
现在我在hello world上遇到了一堆链接器错误:
d:\dev\test> type con >foo.cpp
#include <iostream>
using namespace std;
int main() { cout << "Hello, cling-clong world!" << endl; }
^Z
d:\dev\test> clang++ foo.cpp
foo-839435.o : error LNK2019: unresolved external symbol __ZSt4cout referenced in function _main
foo-839435.o : error LNK2019: unresolved external symbol __ZdlPv referenced in function __ZNSt14error_categoryD0Ev
foo-839435.o : error LNK2019: unresolved external symbol __ZSt18uncaught_exceptionv referenced in function __ZNSo6sentry
D2Ev
foo-839435.o : error LNK2019: unresolved external symbol ___cxa_rethrow referenced in function … 作为另一个问题的答案,我想发布以下代码(也就是说,我想根据这个想法发布代码):
#include <iostream>
#include <utility> // std::is_same, std::enable_if
using namespace std;
template< class Type >
struct Boxed
{
Type value;
template< class Arg >
Boxed(
Arg const& v,
typename enable_if< is_same< Type, Arg >::value, Arg >::type* = 0
)
: value( v )
{
wcout << "Generic!" << endl;
}
Boxed( Type&& v ): value( move( v ) )
{
wcout << "Rvalue!" << endl;
}
};
void function( Boxed< int > v ) {}
int main()
{ …Run Code Online (Sandbox Code Playgroud) 编译器:来自Nuwen发行版的64位MinGW G ++ 4.9.1,在Windows 8.1下.
码:
#ifdef INCLUDE_IOSTREAM
# include <iostream>
#endif
#include <stdio.h> // ::snprintf
#include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE
#include <stdexcept> // std::exception
#ifdef snprintf
# error snprintf defined as macro
#endif
#ifdef _MSC_VER
auto const snprintf = _snprintf;
#endif
void test( double const value, int const precision)
{
char buffer[34];
snprintf( buffer, sizeof( buffer ), "%.*a", precision, value );
printf( "Hex of %.3f with %2d digits: %s\n", value, precision, buffer );
}
auto main() -> int
{ …Run Code Online (Sandbox Code Playgroud) 我想创建一个定义类的一些方法的抽象类.其中一些应该由基类(Base)实现,一些应该在Base中定义但是由Derived覆盖,而其他应该在Base中是纯虚拟的,以强制Derived中的定义.
这当然是抽象类的用途.但是,我的应用程序只会直接使用Derived对象.因此,编译器应该在编译时确切地知道要使用哪些方法.
现在,因为这段代码将在内存非常有限的微控制器上运行,所以我很想避免实际使用带有vtable的虚拟类.从我的测试来看,似乎编译器足够聪明,除非必须,否则不能制作vtable,至少在某些情况下.但是我被告知永远不要相信编译器:是否有可能使这成为编译的必要条件?
以下是一些代码示例:
class Base {
public:
Base() {}
virtual ~Base() {};
virtual int thisMustBeDefined() = 0;
virtual int thisCouldBeOverwritten() { return 10; }
int thisWillBeUsedAsIs() { return 999; }
};
class Derived : public Base {
public:
Derived() {}
~Derived() {}
int thisMustBeDefined() { return 11; }
};
Run Code Online (Sandbox Code Playgroud)
这没有vtable,是我想要的
int main() {
Derived d;
d.thisMustBeDefined();
}
Run Code Online (Sandbox Code Playgroud)
由于我的草率编码,我错误地强迫编译器使用多态,因此需要一个vtable.如何让这个案例抛出错误?
int main() {
Base * d;
d = new Derived();
d->thisMustBeDefined();
}
Run Code Online (Sandbox Code Playgroud)
在这里,我没有在任何时候提到类"Base",因此编译器应该知道所有方法都是在编译时预先确定的.然而,它仍然创造了一个vtable.这是我希望能够通过编译错误检测到这一点的另一个例子.
int main() …Run Code Online (Sandbox Code Playgroud) 更重要的是,这段代码出了什么问题:
#include <assert.h>
#include <functional>
using namespace std;
template< class BaseObjectId >
class Check
{
protected:
Check( function<bool()> const f ) { assert( f() ); }
};
template< int tpMinValue, int tpMaxValue >
class IntegerSubrange
: private Check< IntegerSubrange< tpMinValue, tpMaxValue > >
{
private:
int value_;
public:
enum :int { minValue = tpMinValue, maxValue = tpMaxValue };
static bool rangeContains( int const x )
{
return (minValue <= x && x <= maxValue);
}
operator int() const
{
return …Run Code Online (Sandbox Code Playgroud) 在c ++中,如何打印出堆栈的内容并返回其大小?
std::stack<int> values;
values.push(1);
values.push(2);
values.push(3);
// How do I print the stack?
Run Code Online (Sandbox Code Playgroud) 在我的小型性能问题调查期间,我注意到一个有趣的堆栈分配功能,这里是测量时间的模板:
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
int x; //for simple optimization suppression
void foo();
int main()
{
const size_t n = 10000000; //ten millions
auto start = high_resolution_clock::now();
for (size_t i = 0; i < n; i++)
{
foo();
}
auto finish = high_resolution_clock::now();
cout << duration_cast<milliseconds>(finish - start).count() << endl;
}
Run Code Online (Sandbox Code Playgroud)
现在全部是关于foo()实现,在每个实现中将总共分配500000 ints:
分配在一个块中:
void foo()
{
const int size = 500000;
int a1[size];
x = a1[size - …Run Code Online (Sandbox Code Playgroud)c++ ×10
c++11 ×3
stack ×2
allocation ×1
c ×1
clang ×1
clang++ ×1
compiler-bug ×1
constructor ×1
g++ ×1
hex ×1
inheritance ×1
interface ×1
lambda ×1
performance ×1
printf ×1
standards ×1
std ×1
unique-ptr ×1