我正在寻找一种方法,使方法的用户忽略方法的返回对象/值是非法的。在 C++ 中,这可以通过[[nodiscard]]属性实现。但是,我在 C# 中找不到类似的属性。是否有此类属性的标准实现或有关如何实现此行为的约定?
作为一个例子,为什么这可能有用(并且异常不是一种合适的方法是:
public void foo()
{
if (condition)
{
DoSomeFinalThing();
return;
}
if (othercondition)
{
DoSomeOtherFinalThing();
// whoops, forgot to return here. Common mistake...
}
DefaultAction();
}
Run Code Online (Sandbox Code Playgroud)
是的,这也可以用 来完成else,但我真的不喜欢我的代码中的深层嵌套。
我已经尝试搜索long double的信息,到目前为止,我知道它由编译器以不同的方式实现.
在Ubuntu(XUbuntu)Linux 12.10上使用GCC时,我得到了这个:
double PId = acos(-1);
long double PIl = acos(-1);
std::cout.precision(100);
std::cout << "PId " << sizeof(double) << " : " << PId << std::endl;
std::cout << "PIl " << sizeof(long double) << " : " << PIl << std::endl;
Run Code Online (Sandbox Code Playgroud)
输出:
PId 8 : 3.141592653589793115997963468544185161590576171875
PIl 16 : 3.141592653589793115997963468544185161590576171875
Run Code Online (Sandbox Code Playgroud)
任何人都明白为什么他们输出(几乎)相同的东西?
我正在尝试使用共享库来构建模块化程序.
有两个要编译的cpp文件:
共享库,用.编译
g ++ -fPIC -shared module.cpp -o module.so
//module.cpp
#include <iostream>
Run Code Online (Sandbox Code Playgroud)
文件使用共享库,编译用
g ++ src/main.cpp -ldl -o binary
要么
g ++ -DFIX src/main.cpp -ldl -o binary
//main.cpp
#include <dlfcn.h>
#ifdef FIX
# include <iostream>
#endif
int main()
{
void* h = dlopen("./module.so", RTLD_LAZY);
if ( h )
{
dlclose(h);
}
}
Run Code Online (Sandbox Code Playgroud)
使用FIXundefined,valgrind会报告很多仍然可访问的内存(5,373bytes),如果已FIX定义,则不会泄漏任何内存.
iostream在共享库中使用有什么问题?
g ++ - 4.6,g ++ - 4.7和g ++ - 4.8会出现此问题.g ++ - 4.4不显示此行为.遗憾的是,我没有其他编译器可以测试(我不想因此而切换到g ++ - 4.4).
更新:
使用其他标志编译共享库文件-static-libstdc++ -static-libgcc …
这是我给出分段错误的代码
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(void) {
char *get;
scanf("%s", get);
int k = strcmp("sachin", get);
printf("%d", k);
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助;
根据GCC C++ 11支持状态网站,强类型枚举可用于g++4.4更高版本.
但是以下内容不能编译g++4.4:
enum class Foo
{
value_1,
value_2
};
int main()
{
Foo a = Foo::value_1;
Foo b = Foo::value_2;
const bool test = ( a < b );
}
Run Code Online (Sandbox Code Playgroud)
错误消息是error: invalid operands of types ‘Foo’ and ‘Foo’ to binary ‘operator<’.
接受该代码编译器包括g++-4.6,g++-4.7,g++-4.8和clang++ 3.2.(我无法测试,g++-4.5因为我目前没有安装它(而Ubuntu 13不希望我)
我可以轻松地为这个(相当旧的)编译器提供一个带有宏的回退,但我通常不喜欢它(它停在哪里?...).
这有什么问题?支持信息是错误还是缺少一些不包含在"支持强类型枚举"中?我能想到的最后一个选项:我的代码中存在问题吗?
我想专门化一个功能模板.此函数在命名空间中声明:
namespace foo
{
template <int>
void function();
}
Run Code Online (Sandbox Code Playgroud)
(为简单起见,模板基于a int,而在我的生产代码中,它是一个enum class,但它是同一个问题.基于类型的模板也是如此)
现在我想专门针对特定值:
template <>
void foo::function<0>()
{
}
Run Code Online (Sandbox Code Playgroud)
这无法编译g++ -std=c++11(版本4.6,4.7,4.8和4.9):
'template void foo :: function()'在不同命名空间中的特殊化[-fpermissive]
clang++ -std=c++11 接受此代码.
g ++也接受以下部分:
namespace foo
{
template <>
void function<0>()
{
}
}
Run Code Online (Sandbox Code Playgroud)
谁是对的,gcc还是clang?
我正在尝试构建一个正确的Makefile.
我想要的是完全控制正在发生的事情,所以我不想要任何第三方软件.
我当前的尝试对我来说似乎是逻辑,但由于依赖关系生成无效,我有点卡住了.
为了更好的可读性,完整的Makefile被分成几小块.如果有什么需要改进,我将不胜感激任何评论.
首先,我有以下静态定义
CXX = g++
CXXFLAGS = -Wall \
-Wextra \
-Wuninitialized \
-Wmissing-declarations \
-pedantic \
-O3 \
-p -g -pg
LDFLAGS = -p -g -pg
DEPFLAGS = -MM
Run Code Online (Sandbox Code Playgroud)
Afaik这应该没问题.使分析标志可选是完美的,但这并不重要.
SRC_DIR = ./src
OBJ_DIR = ./obj
SRC_EXT = .cpp
OBJ_EXT = .o
TARGET = ./bin/my_target
SRCS = $(wildcard $(SRC_DIR)/*$(SRC_EXT))
OBJS = $(subst $(SRC_DIR), $(OBJ_DIR), $(SRCS:$(SRC_EXT)=$(OBJ_EXT)))
DEP = depend.main
Run Code Online (Sandbox Code Playgroud)
基本上,这应该只是提取所有*.cpp文件出的子文件夹中src,另外更换./src用./obj,并.cpp与.o作为对象的名称.
.PHONY: clean all depend
all: …Run Code Online (Sandbox Code Playgroud) 今天我浏览了一些源代码(这是一个解释软件框架使用的示例文件)并发现了很多这样的代码:
int* array = new int[10]; // or malloc, who cares. Please, no language wars. This is applicable to both languages
for ( int* ptr = &(array[0]); ptr <= &(array[9]); ptr++ )
{
...
}
Run Code Online (Sandbox Code Playgroud)
所以基本上,他们已经完成了"获取位于地址的对象的地址array + x".
通常我会说,这是明显的愚蠢,因为写作array + 0或array + 9直接做同样的事情.我甚至总是会将这样的代码重写为size_t for循环,但这是一个风格问题.
但过度使用这个让我思考:我是否遗漏了一些明显的东西或者隐藏在语言黑暗角落里的东西?
对于任何想要查看原始源代码的人来说,有了它所有令人讨厌的gotos,mallocs当然还有这个指针的东西,请随时在线查看.
在我的多线程应用程序中,我有一个可以简化为此示例的条件
std::atomic<bool> a, b;
// ...
if ( a.load() && b.load() )
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
显然,在条件之后,a和b可以保持不同的值.
在我的应用程序中,它认为,如果两个值同时为真,则它们不能再次改变状态.但是在a.load()返回之后,true它甚至可能在b.load()评估之前改变其值.
是否有一个优雅的解决方案来原子评估这个陈述?显然锁定a.store(..)和b.store(..)的每个调用都可以在这里工作,但这远非好.
我试图使用函数模板foo将参数转换为initializer_list.但是,initializer_list它转换后的奇怪值与输入参数不同.
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
template<class T>
void func(std::initializer_list<T> a_args)
{
if (a_args.begin() != a_args.end())
{
auto last = prev(a_args.end());
copy(a_args.begin(), last, ostream_iterator<int>(cout, ","));
cout << *last;
}
cout << endl;
}
template<class T, class ...Args>
struct first_of
{
typedef T type;
};
template<class ...Args>
initializer_list<typename first_of<Args...>::type> foo(Args&&... args)
{
return { forward<Args>(args)... };
}
int main()
{
func({1,2,3});
auto x = foo(1,2,3);
func(x); //this should be …Run Code Online (Sandbox Code Playgroud)