我最近在cppreference 中看到了这段代码:
string str="global scope";
void main()
{
string str="main scope";
if (true){
string str="if scope";
cout << str << endl;
}
cout << str << endl;
}
Run Code Online (Sandbox Code Playgroud)
哪个输出:
if scope
main scope
Run Code Online (Sandbox Code Playgroud)
这很好,我了解整个嵌套作用域的事情,并且我知道当堆栈在语句末尾展开它时, if 作用域内的“str”将被销毁,因此此后它将不可用,因此第二个print 将主“str”作为其参数。
但是,我知道主“str”实际上在 IF 内部可用,或者至少应该是,但问题是如何从 IF 语句内部访问主“str”?
我如何从 main 和/或 if 内部访问全局“str”?
我知道使用不同的名称会更简单,但这个问题不是为了特定的实际应用,而是为了更好地理解 c++ 范围。
如果我为一个向量保留了一些空间,然后我用它复制了一些值std::copy_n(),我得到正确复制并可访问的值,但向量的大小仍为零.这是预期的行为吗?我应该调整矢量大小,即使它没有效率吗?
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<double> src, dest;
for(double x = 0.0; x < 100.0; ++x)
src.push_back(x);
dest.reserve(src.size());
std::copy_n(src.cbegin(), src.size(), dest.begin());
std::cout << "src.size() = " << src.size() << std::endl;
std::cout << "dest.size() = " << dest.size() << std::endl;
for(size_t i = 0; i < src.size(); ++i)
std::cout << dest[i] << " ";
}
Run Code Online (Sandbox Code Playgroud)
编译器测试:clang,gcc,Visual C++
在"C++入门"一书中,有一节关于类声明和定义.我不明白这句话的一切:
只有在定义了类时,才能将数据成员指定为类类型.
我不明白这句话背后的逻辑.如何将数据成员指定为类类型,此操作的含义是什么?
以下程序编译正常.
#include <iostream>
#include <bitset>
void foo(std::bitset<10> n)
{
std::cout << n.size() << "\n";
}
int main()
{
std::bitset<10> n;
foo(n);
}
Run Code Online (Sandbox Code Playgroud)
$ g++ -std=c++11 -Wall -Wextra -pedantic foo.cpp
$ ./a.out
10
Run Code Online (Sandbox Code Playgroud)
如何修改foo()函数以使其可以接受bitset任何大小?
鉴于以下代码
#include <iostream>
using namespace std;
template <typename Type>
struct Something {
Something() {
cout << "Something()" << endl;
}
template <typename SomethingType>
Something(SomethingType&&) {
cout << "Something(SomethingType&&)" << endl;
}
};
int main() {
Something<int> something_else{Something<int>{}};
auto something = Something<int>{};
Something<int>{something};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
Something()
Something()
Something(SomethingType&&)
Run Code Online (Sandbox Code Playgroud)
为什么复制构造函数被解析为模板化转发引用构造函数而不是移动构造函数?我猜这是因为移动构造函数是隐式定义的,而不是复制构造函数。但是在阅读了堆栈溢出中未隐式定义复制构造函数的情况后,我仍然感到困惑。
所以这是一个非常基本的问题和超级微不足道的但我只是通过c ++中的编程原则和实践,我的程序用于读取字符串和int的行为与Bjarne Stroustrup编写的书不同,所以如果他感到惊讶犯了一个错误.无论如何这里是代码:
#include "..\std_lib_facilities.h"
int main()
{
cout << "Please enter your first name and age\n";
string first_name = "???"; // string variable
// ("???” means “don’t know the name”)
int age = -1; // integer variable (1 means “don’t know the age”)
cin >> first_name >> age; // read a string followed by an integer
cout << "Hello, " << first_name << " (age " << age << ")\n";
}
Run Code Online (Sandbox Code Playgroud)
当我在提示符输入"22 Carlos"时它输出"Hello,22(0岁)",基本上使我的错误检查的初始化值无用.这是c ++或其他东西的新功能,这就是为什么这本书出错了?
编辑1:BTW我在Windows 7上使用GCC for cygwin,并且-std = …
今天,我遇到了一些我对复制构造函数不太了解的内容。
考虑下面的代码:
#include <iostream>
using namespace std;
class some_class {
public:
some_class() {
}
some_class(const some_class&) {
cout << "copy!" << endl;
}
some_class call() {
cout << "is called" << endl;
return *this; // <-- should call the copy constructor
}
};
some_class create() {
return some_class();
}
static some_class origin;
static some_class copy = origin; // <-- should call the copy constructor
int main(void)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后在将原点分配给副本时调用副本构造函数,这很有意义。但是,如果我将复制声明更改为
static some_class copy = some_class();
Run Code Online (Sandbox Code Playgroud)
它没有被调用。即使在使用该create()函数时,它也不会调用复制构造函数。但是,将其更改为
static …Run Code Online (Sandbox Code Playgroud) 因此,以最简化的形式,我正在进行类似的事情,
template <class T>
bool f(const T &a, const T &b, std::function<bool(const T&, const T&)> func)
{
return func(a,b);
}
template <class T>
bool g(const T &a, const T &b)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
但是,任何试图调用f(),任何事情,f('a', 'b', g),f(1, 2, g),结果总是在,无论我是否通过变量为const引用或只是简单的值或任何“呼叫到‘F’没有匹配功能”。我以为它无法推断出某些模板,但是我不知道在哪里或为什么。
我承认,我对如何使用函数对象有一个非常脆弱的把握,这样做是否可能?
我有以下代码:
#include <utility>
template<class T,class E = void>
struct func_impl;
template<class T,class E = void>
constexpr inline bool has_func = false;
template<class T>
constexpr inline bool has_func<T,decltype(func_impl<T>::apply(std::declval<T>()))> = true;
template<>
struct func_impl<int>
{
static int apply(int i);
};
static_assert(has_func<int>);
Run Code Online (Sandbox Code Playgroud)
static_assert失败,我预计它会成功.我做错了什么?
我有一个处理不同容器的通用函数。
template<template<class, class> class C, class T, class A>
void handle(C<T, A> const& c)
{
cout << "General handling\n";
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我将自定义容器传递给它,我希望它做出不同的反应。为简单起见,我首先尝试以单独的方式处理向量,尝试将此函数部分专门化为向量。
这就是我认为它应该是什么样子。
template<class T, class A>
void handle<std::vector>(std::vector<T, A> const& c)
{
cout << "vector handling\n";
}
Run Code Online (Sandbox Code Playgroud)
但是gcc给出了以下错误:
无法执行程序编译器返回:1 编译器标准错误:16:36:错误:模板 id 'handle class std::vector>' in declaration of primary template 16 | (std::vector const& c) |
这可以通过部分模板特化来完成吗?
c++ ×10
templates ×4
c++11 ×3
c++14 ×2
c++17 ×1
cin ×1
constructor ×1
copy ×1
copy-elision ×1
declaration ×1
definition ×1
name-hiding ×1
nested ×1
scope ×1
std-function ×1
stdvector ×1